This one popes up quite frequently
If you attempt to access an Active Directory object that doesn’t exist you get an error.
£> Get-ADUser Richardp
Get-ADUser : Cannot find an object with identity: ‘Richardp’ under: ‘DC=Manticore,DC=org’.
At line:1 char:1
+ Get-ADUser Richardp
+ ~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Richardp:ADUser) [Get-ADUser], ADIdentityNotFoundException
+ FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException,M
icrosoft.ActiveDirectory.Management.Commands.GetADUser
Just using the Erroraction option isn’t going to help
£> Get-ADUser Richardp -ErrorAction SilentlyContinue
Get-ADUser : Cannot find an object with identity: ‘Richardp’ under: ‘DC=Manticore,DC=org’.
At line:1 char:1
+ Get-ADUser Richardp -ErrorAction SilentlyContinue
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Richardp:ADUser) [Get-ADUser], ADIdentityNotFoundException
+ FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException,M
icrosoft.ActiveDirectory.Management.Commands.GetADUser
You could do something like this
$ErrorActionPreference = ‘SilentlyContinue’
Get-ADUser Richardp
$ErrorActionPreference = $pref
But a better approach is to use a try-catch block to solve the problem
£> try {
Get-ADUser Richardp -ErrorAction Stop
}
catch {
Write-Warning "User Not Found"
}
WARNING: User Not Found
Instead of issuing the warning perform some other action as appropriate