15
votes

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!!!!"}
2

2 Answers

18
votes

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!!!!" }
10
votes

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.