user3175140
user3175140

Reputation: 531

PowerShell -ErrorAction SilentlyContinue Does not work with Get-ADUser

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

Upvotes: 16

Views: 33416

Answers (2)

Garrett
Garrett

Reputation: 131

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.

Upvotes: 13

mjolinor
mjolinor

Reputation: 68321

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

Upvotes: 22

Related Questions