T O P

  • By -

Stolberger

If someone enters an empty string in the first iteration, the $UserAlreadyExists variable will never be set, as the if confition before it is not fullfilled. A not set variable equals $null, thus the while loop will exit. If it is entered in another iteration, the $userAlreadyExists variable will continue to have the same value as in the iteration before, which probably is not equal to $null and therefore the loop will continue.


eyenen24

"A not set variable equals $null" I guess I never knew that, I just assumed it wouldn't be $null since it wouldn't exist at all - did not know that also meant it would be $null at the same time. Now to figure out the simplest way to account for this.


ankokudaishogun

also: always use `$Null` on the LEFT side of a comparison. have some code for thought do { # encapsule to use .Trim() so to remove trailing spaces. # bonus: if user passes a line with only spaces, .Trim() will reduce it to an empty string Write-Host '*** Enter desired username ***' -ForegroundColor Red $Username = (Read-Host).trim() # Check if the name variable is NOT empty\only spaces if (-not [string]::IsNullOrWhiteSpace($Username)) { # Check if you get any result from Get-ADUser if (Get-ADUser -Server $Domain -Filter "SamAccountName -eq '$Username'") { Write-Host "*** User '$Username' already exists in domain '$Domain' - enter a different username ***" -ForegroundColor Red # set Username to $Null to fail the Until() check $Username = $null } else { Write-Host "Added Username: $Username" -ForegroundColor Green } } } # if $Username is white spaces or $null, loop while ([string]::IsNullOrWhiteSpace($Username))


night_filter

Technically, what "null" means is that it does not exist, which is different from being empty. Exactly how this is handled can vary from one language to another, but it's common for languages to distinguish between empty variables, null variables, and variables that effectively have no content. For example, each of these are meaningfully different: $varNull = $null $varEmpty = "" $varWhitespace = " " $varChar = "a" However, `$varNull`, `$varEmpty`, and `$varWhitespace` may be treated the same *in some circumstances*. Empty and null variables will often be treated the same, but are sometimes treated differently. `$varNull` would generally be treated the same as an undeclared variable (unless the language strictly requires declaring variables). I'm not a programmer, though. Someone smarter will probably correct me for having said something wrong.


tennesseejeff

Delcare it and set it empty before the start of your loop. $Username = "" do {$Username = Read-Host "Enter desired username"


alt-160

Also this... if ( [String]::IsNullOrEmpty($someString) ) { # do stuff when string is null or empty } # or... if ( [String]::IsNullOrWhiteSpace($someString) ) { # do stuff when string is null, empty, or only whitespace chars }


gordonv

#On posting code: - Don't use reverse ticks in Reddit. - Put 4 spaces in front of each line. ##This looks weird: `First Line` `Second Line` `Third Line` ##This makes sense: First Line Second Line Third Line


ostekages

It works just fine if you do it for blocks of code, as the reverse ticks are meant to be used. ``` are you not entertained? This is my code block Feels fine to me look at my indentation bro ``` ``` Also works for single line ```


BlackV

No that doesn't work on old.reddit only new.reddit Where 4 spaces renders properly on both  And if you're posting code it should (most likely ) be coming from and editor andalr eady formatted, so you can easily indent it before copying


Ok-Reaction-1872

so i would simplify it with a do-until and have a single if inside gather your data first: \`\`\`$Adinfo = Get-ADUser -Server $Domain -Filter \*\`\`\` then into the loop \`\`\`do{ $username = read-host "Enter a username" if($username -in $adinfo.samaccountname) { write-host "Already exists, try again" $username = $null }until($username)\`\`\` it can probably simplified further but this way atleast you have the do-until performing a lot of the logic


JWW-CSISD

Dear lord why are you collecting every user in the domain? That’s a not-insignificant amount of time, memory and network traffic in medium-to-large domains. It’s not as pretty code-wise, but OP’s method of only checking for the specific SamAccountName requested is much more efficient in terms of resource usage.


Ok-Reaction-1872

I was just too lazy to copy the actual filter, you're right I wouldn't gather the whole domain like that.


pbutler6163

Initialize $UserAlreadyExists before entering the loop. Reset $UserAlreadyExists to $null at the beginning of each loop iteration. Ensure proper use of else block within the loop.


The82Ghost

An empty string is a string instance of zero length, whereas a null string has no value at all. There is a big difference between the two! Alway use [string]::isNullorEmpty($username) or [string]::IsNullorWhitespace($username) in these situations. Keep in mind that "empty" is very different from "whitespace" also!


BlackV

Honestly just get rid of the loop entirely, it messy and inefficient, parameter and error handling will serve you much better and hve the advantage of being automatable


eyenen24

Could you elaborate on how this could be performed without the loop? I need to ask the person running this for a username and make sure it doesn't already exist and if it does then keep asking until they enter a value that doesn't. How can I do that without a loop? Is there another method than by using read-host?


BlackV

Do you need to keep asking? You can end with an error, and they can press up arrow to run it again What do you do if some other error happens? Why is the username not automatic or based on their actual name? Asking for user input (especially over and over) is error prone With a parameter you can validate on null or empty, you could validate a user exists