SOLVED

Testing for username not working

%3CLINGO-SUB%20id%3D%22lingo-sub-3201350%22%20slang%3D%22en-US%22%3ETesting%20for%20username%20not%20working%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3201350%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20All%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20have%20a%20code%20block%20I%20am%20trying%20to%20get%20working%20to%20test%20if%20a%20username%20is%20already%20created%2C%20then%20move%20to%20the%20next.%26nbsp%3B%20If%20its%20not%20existing%2C%20set%20variable%20%24UPN%20to%20that%20username..%20however%2C%20something%20isn't%20working%20in%20the%20test%20and%20tries%20to%20use%20the%20first%20option%20every%20time%2C%20even%20if%20its%20already%20an%20existing%20account.%20The%20FI%20and%20LN%20are%20provided%20based%20on%20content%20of%20the%20CSV%20file%20and%20that%20logic%20is%20not%20included%20as%20that%20does%20work.%26nbsp%3B%20Any%20help%20would%20be%20greatly%20appreciated%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%23%23Check%20for%20username%20using%20firstl%20(first%20name%20%26amp%3B%20last%20initial)%0A%24SAM%20%3D%20%22%24FI%24LN%22%0A%24UPN%20%3D%20%22%24Email%20address%20removed%22%0AWrite-host%20%22Creating%20User%20%24UPN%22%20-foregroundcolor%20Cyan%0Aif%20(%24(try%20%7Bget-ADUser%20-Identity%20%24UPN%7D%20catch%20%7B%24null%7D))%0A%20%7B%20%23%23Check%20for%20username%20using%20flast%20(first%20initial%20%26amp%3B%20last%20name)%0A%20%20%20%24SAM%20%3D%20%22%24FN%24LI%22%0A%20%20%20%24UPN%20%3D%20%22%24SAM%20%40%20something.org%22%0A%20%20%20%20if%20(%24(try%20%7Bget-ADUser%20-Identity%20%24UPN%7D%20catch%20%7B%24null%7D))%0A%20%20%20%20%20%7B%20%23%23Check%20for%20username%20using%20fflast%20(first%20two%20initials%20%26amp%3B%20last%20name)%0A%20%20%20%20%20%20%20%24SAM%20%3D%20%22%24FI2%24LN%22%0A%20%20%20%20%20%20%20%24UPN%20%3D%20%22%24SAM%20%40%20something.org%22%0A%20%20%20%20%20%20%20if%20(%24(try%20%7Bget-ADUser%20-Identity%20%24UPN%7D%20catch%20%7B%24null%7D))%0A%20%20%20%20%20%20%20%20%20%7B%20%23%23Check%20for%20username%20using%20lastf%20(last%20name%20%26amp%3B%20first%20initial)%0A%20%20%20%20%20%20%20%20%20%20%20%24SAM%20%3D%20%22%24LN%24FI%22%0A%20%20%20%20%24UPN%20%3D%20%22%24SAM%20%40%20something.org%22%0A%20%20%20%20if%20(%24(try%20%7Bget-ADUser%20-Identity%20%24UPN%7D%20catch%20%7B%24null%7D))%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%7B%20Write-host%20%22Error%20-%20Unable%20to%20find%20a%20unique%20name%20for%20the%20account%22%20-foregroundcolor%20Red%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%24UPN%20%3D%20%22123454321%20%40%20something.org%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%24ID%20%3D%20%22123454321%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20else%20%7B%20Write-host%20%22The%20UPN%20will%20be%20%24UPN%22%20-foregroundcolor%20Cyan%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Write-output%20%22%24ID%2C%24UPN%22%20%7C%20out-file%20%24BFile%20-append%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20else%20%7B%20Write-host%20%22The%20UPN%20will%20be%20%24UPN%22%20-foregroundcolor%20Cyan%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20Write-output%20%22%24ID%2C%24UPN%22%20%7C%20out-file%20%24BFile%20-append%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%7D%0A%20%20%20else%20%7B%20Write-host%20%22The%20UPN%20will%20be%20%24UPN%22%20-foregroundcolor%20Cyan%0A%20%20%20%20%20%20%20%20%20%20Write-output%20%22%24ID%2C%24UPN%22%20%7C%20out-file%20%24BFile%20-append%0A%20%20%20%20%20%20%20%20%7D%0A%20%7D%0Aelse%20%7B%20Write-host%20%22The%20UPN%20will%20be%20%24UPN%22%20-foregroundcolor%20Cyan%0A%20%20%20%20%20%20%20Write-output%20%22%24ID%2C%24UPN%22%20%7C%20out-file%20%24BFile%20-append%0A%20%20%20%20%20%7D%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-3201350%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EPowerShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3202855%22%20slang%3D%22en-US%22%3ERe%3A%20Testing%20for%20username%20not%20working%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3202855%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F339118%22%20target%3D%22_blank%22%3E%40caseyj%3C%2FA%3E%26nbsp%3BDid%20a%20script%20like%20this%20before%2C%20but%20used%26nbsp%3Bcatch%20%5BMicrosoft.ActiveDirectory.Management.ADIdentityNotFoundException%5D%20.%20Don't%20know%20if%20that%20changes%20things%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3204092%22%20slang%3D%22en-US%22%3ERe%3A%20Testing%20for%20username%20not%20working%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3204092%22%20slang%3D%22en-US%22%3EI%20did%20try%20that%2C%20however%2C%20it%20didnt%20seem%20to%20help%20change%20the%20outcome...%20I%20thought%20I%20could%20use%20that%20catch%20you%20used%20but%20no%20luck.%3C%2FLINGO-BODY%3E
Occasional Contributor

Hi All,

 

I have a code block I am trying to get working to test if a username is already created, then move to the next.  If its not existing, set variable $UPN to that username.. however, something isn't working in the test and tries to use the first option every time, even if its already an existing account. The FI and LN are provided based on content of the CSV file and that logic is not included as that does work.  Any help would be greatly appreciated:

 

##Check for username using firstl (first name & last initial)
$SAM = "$FI$LN"
$UPN = "$Email address removed"
Write-host "Creating User $UPN" -foregroundcolor Cyan
if ($(try {get-ADUser -Identity $UPN} catch {$null}))
 { ##Check for username using flast (first initial & last name)
   $SAM = "$FN$LI"
   $UPN = "$SAM @ something.org"
    if ($(try {get-ADUser -Identity $UPN} catch {$null}))
     { ##Check for username using fflast (first two initials & last name)
       $SAM = "$FI2$LN"
       $UPN = "$SAM @ something.org"
       if ($(try {get-ADUser -Identity $UPN} catch {$null}))
         { ##Check for username using lastf (last name & first initial)
           $SAM = "$LN$FI"
	   $UPN = "$SAM @ something.org"
	   if ($(try {get-ADUser -Identity $UPN} catch {$null}))
             { Write-host "Error - Unable to find a unique name for the account" -foregroundcolor Red
               $UPN = "123454321 @ something.org"
               $ID = "123454321"
             }
           else { Write-host "The UPN will be $UPN" -foregroundcolor Cyan
                  Write-output "$ID,$UPN" | out-file $BFile -append
                }
          }
       else { Write-host "The UPN will be $UPN" -foregroundcolor Cyan
              Write-output "$ID,$UPN" | out-file $BFile -append
            }
     }
   else { Write-host "The UPN will be $UPN" -foregroundcolor Cyan
          Write-output "$ID,$UPN" | out-file $BFile -append
        }
 }
else { Write-host "The UPN will be $UPN" -foregroundcolor Cyan
       Write-output "$ID,$UPN" | out-file $BFile -append
     }

 

4 Replies

@caseyj Did a script like this before, but used catch [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException] . Don't know if that changes things?

I did try that, however, it didnt seem to help change the outcome... I thought I could use that catch you used but no luck.
That's too bad... I only checked the samaccountname in my script, if it didn't already exist the user could be created.. Error-handling is hard sometimes... You could try to set variables for each check you want to do and if it's not $null, you can set an action on that? I don't see the whole script of course, difficult to oversee the if else construction like that..
best response confirmed by caseyj (Occasional Contributor)
Solution
It's not going to be null though if it doesn't exist, so the catch isn't going to work. It will have the ADIdentityNotFoundException which does work in a catch as I have it in several scripts.

However, I've done something similar to this in the past and instead of using Get-ADUser, I used Get-ADObject and a filter which means you can use null and no longer need the try\catch. (Tip: fully test your filter).