我的脚本中有一个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
答案 0 :(得分:2)
尝试使用像这样的try{} ... catch{}
块
try
{
// your code to find AD group goes here
}
catch {
//whatever you want to do further
}
另一种方法是使用-Filter
命令行开关的Get-ADUser
选项