Forum Discussion
caseyj
Feb 22, 2022Copper Contributor
Testing for username not working
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, somethi...
- Feb 25, 2022It'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).
caseyj
Feb 23, 2022Copper Contributor
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.
Feb 23, 2022
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..