Im having issues getting -ErrorAction SilentlyContinue to work with cmdlet 'Get-ADUser'
This doesn't work, the error is displayed with or without -ErrorAction?
get-aduser "JSmith" -ErrorVariable Err -ErrorAction SilentlyContinue
if ($Err){write-host "This is an error!!!!"}
This works (No error is display and silently continues, under the same conditions?
get-childitem z: -ErrorVariable Err -ErrorAction SilentlyContinue
if ($Err){write-host "This is an error!!!!"}
The get is actually performed at the DC by the gateway service, and the error handling doesn't work quite the same. Fortunately Try/Catch does work:
Try { get-aduser "JSmith" }
Catch { write-host "This is an error!!!!" }
What mjolinor is saying about the explicit filter is the following works:
$Sam = "JSmith"
$userObj = get-aduser -filter {SamAccountName -eq $Sam} -erroraction silentlycontinue
$userObj
will be null if the user is not found. This allows the code to address the not found condition with out using the try/catch.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With