我该如何解决此PowerShell错误?

时间:2013-08-08 20:03:52

标签: powershell-v3.0

我的脚本中有一个If语句,检查AD对象是否已存在。但是,这些评估都不会阻止主机打印不需要的评估“找不到目录对象”。

!$(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

1 个答案:

答案 0 :(得分:2)

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

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

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

请参阅Get-ADUser Commandlet