Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell -ErrorAction SilentlyContinue Does not work with Get-ADUser

Tags:

powershell

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!!!!"}
like image 689
user3175140 Avatar asked Apr 10 '14 22:04

user3175140


2 Answers

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!!!!" }
like image 104
mjolinor Avatar answered Nov 13 '22 04:11

mjolinor


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.

like image 11
Garrett Avatar answered Nov 13 '22 03:11

Garrett