Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

PowerShell nested Try/Catch

$
0
0

Sorry if this is not posted in the right location.

I have a feeling there is a better way to do this. If so, I'm all ears. Anyway, I want to search for an AD user with and without a space in their name because sAMAccount names can be either format.



This code:

1)search for name with space

2) if 1 errors: search for name without space

3) if 2 errors: do things when the user is not found by either search method like add the user.

even when the user is found, the code always executes the 2nd catch block, giving me an "ADUserAlreadyExists" exception. I wouldn't expect this because the 2nd catch should only execute if the 2nd try encounters an error. I think it has to do with both catches being for the same error name. 


Viewing all articles
Browse latest Browse all 15028

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>