SOLVED

Testing for username not working

Copper 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 (Copper 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).
1 best response

Accepted Solutions
best response confirmed by caseyj (Copper 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).

View solution in original post