0

I have an If statement in my script checking if an AD Object already exists. However, none of these evaluations prevent the host from printing the undesired evaluation "Directory object not found."

!$(Get-ADObject -Identity $ImportedObject.DistinguishedName -ErrorAction SilentlyContinue)


[void](!$(Get-ADObject -Identity $ImportedObject.DistinguishedName))


!$($(Get-ADObject -Identity $ImportedObject.DistinguishedName) | Out-Null)


$ErrorAction = $CurrentErrorAction
$ErrorAction = "SilentlyContinue"
!$(Get-ADObject -Identity $ImportedObject.DistinguishedName)
$ErrorAction = $CurrentErrorAction
4

1 回答 1

2

尝试使用try{} ... catch{}这样的块

try 
{
// your code to find AD group goes here
}
  catch  {
      //whatever you want to do further
     }

另一种方法是使用命令行-Filter选项Get-ADUser

请参阅Get-ADUser 命令行开关

于 2013-08-08T20:09:29.210 回答