While Loop with More than One Condition? Need Help.

%3CLINGO-SUB%20id%3D%22lingo-sub-2780473%22%20slang%3D%22en-US%22%3EWhile%20Loop%20with%20More%20than%20One%20Condition%3F%20Need%20Help.%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2780473%22%20slang%3D%22en-US%22%3E%3CP%3EI%20already%20have%20a%20script%20the%20automates%20the%20creation%20of%20users.%26nbsp%3B%20If%20the%20sAMAccountName%20is%20not%20unique%2C%20then%20My%20script%20has%20a%20while%20loop%20that%20Basically%20says%3C%2FP%3E%3CP%3E%3CSPAN%3Eif%20(%24user%20-eq%20%24null)%20%7B%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%3CSPAN%3E%24i%20%3D%201%3CBR%20%2F%3Ewhile%20(((Get-ADUser%20-Filter%20%7BsAMAccountName%20-eq%20%24sAMAccountName%7D)%20-ne%20%24null)%20%7B%3CBR%20%2F%3E%24sAMAccountName%20%3D%20%24sAMAccountName%20%2B%20%24i%3CBR%20%2F%3E%24i%2B%2B%3CBR%20%2F%3E%7D%3CBR%20%2F%3E%24i--%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%3EI%20recently%20discovered%20an%20issue%2C%20that%20is%20not%20addressed%20with%20this..%20If%20the%20AD-Object%20itself%2C%20for%20the%20user%20is%20the%20SAME%20name%20in%20the%20same%20OU%2C%20the%20user%20cannot%20be%20created%20of%20course.%26nbsp%3B%20%26nbsp%3BIf%20they%20are%20in%20a%20different%20OU%20they%20can.%26nbsp%3B%20The%20other%20day%20I%20had%202%20users%20with%20the%20same%20NAME%2C%20and%20even%20though%20the%20sams%20account%20is%20unique%2C%20my%20code%20doesn't%20address%20when%20the%20Object%20name%20is%20the%20same%20as%20well.%26nbsp%3B%20I%20tried%20this%2C%20but%20can't%20seem%20to%20get%20it%20to%20work%3A%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%3CSPAN%3E(This%20finds%20any%20user%20in%20that%20OU%20path%20that%20has%20the%20SAME%20Object%20name)%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%3E%24GetSameName%20%3D%20Get-ADUser%20-Filter%20*%20-SearchBase%20%24path%20-Properties%20Name%20%7C%20Where-Object%7B%24_.Name%20-and%20(%24name%20-contains%20%24_.Name)%7D%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%3Eif%20(%24user%20-eq%20%24null)%20%7B%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%3CSPAN%3E%24i%20%3D%201%3CBR%20%2F%3Ewhile%20(((Get-ADUser%20-Filter%20%7BsAMAccountName%20-eq%20%24sAMAccountName%7D)%20-ne%20%24null)%20-and%20(%20%24name%20-eq%20%24GetSameName%20))%20%7B%3CBR%20%2F%3E%24sAMAccountName%20%3D%20%24sAMAccountName%20%2B%20%24i%3CBR%20%2F%3E%24i%2B%2B%3CBR%20%2F%3E%7D%3CBR%20%2F%3E%24i--%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHowever%2C%20I%20can't%20figure%20out%20how%20to%20incorporate%20this%20into%20my%20while%20loop.%20I%20basically%20want%20it%20to%20hit%20the%20incrementation%20loop%2C%26nbsp%3B%20if%20the%20sAMAccountName%2C%26nbsp%3B%20or%20if%20the%20Object%20Name%20is%20not%20unique%20in%20that%20OU.%20So%20Check%20the%20sAMAccount%20name%2C%20if%20not%20unique%2C%20hit%20the%20incantation%20look.%26nbsp%3B%20If%20the%20sAMSAccount%20name%20IS%20unique%2C%20but%20the%20ADObject%20Name%20is%20still%20not%2C%20then%20STILL%20hit%20some%20sort%20of%20incrementation%20loop%20that%20leaves%20the%20Unique%20sAMSAccount%20name%20alone%2C%20BUT%20increments%20the%20%24name%2C%20that%20will%20be%20used%20in%20the%20Unique%20creation%20of%20the%20AD-Object.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2780473%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EPowerShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
New Contributor

I already have a script the automates the creation of users.  If the sAMAccountName is not unique, then My script has a while loop that Basically says

if ($user -eq $null) {

$i = 1
while (((Get-ADUser -Filter {sAMAccountName -eq $sAMAccountName}) -ne $null) {
$sAMAccountName = $sAMAccountName + $i
$i++
}
$i--

 

I recently discovered an issue, that is not addressed with this.. If the AD-Object itself, for the user is the SAME name in the same OU, the user cannot be created of course.   If they are in a different OU they can.  The other day I had 2 users with the same NAME, and even though the sams account is unique, my code doesn't address when the Object name is the same as well.  I tried this, but can't seem to get it to work:

(This finds any user in that OU path that has the SAME Object name)

 

$GetSameName = Get-ADUser -Filter * -SearchBase $path -Properties Name | Where-Object{$_.Name -and ($name -contains $_.Name)}

 

if ($user -eq $null) {

$i = 1
while (((Get-ADUser -Filter {sAMAccountName -eq $sAMAccountName}) -ne $null) -and ( $name -eq $GetSameName )) {
$sAMAccountName = $sAMAccountName + $i
$i++
}
$i--

 

However, I can't figure out how to incorporate this into my while loop. I basically want it to hit the incrementation loop,  if the sAMAccountName,  or if the Object Name is not unique in that OU. So Check the sAMAccount name, if not unique, hit the incantation look.  If the sAMSAccount name IS unique, but the ADObject Name is still not, then STILL hit some sort of incrementation loop that leaves the Unique sAMSAccount name alone, BUT increments the $name, that will be used in the Unique creation of the AD-Object.

 

1 Reply
An option for you to consider.
Maybe check that the SAMAccountName is globally unique across AD, and make the samaccountname and name properties the same for each user.