2

我有一个创建新 AD 对象的脚本(通过 New-ADObject,碰巧)。如果对象已经存在,我需要捕获并处理它。但是,异常类型并不像FullyQualifiedErrorId 那样明确。请注意以下事项:

> $Error[-1] | Format-List -Property * -Force

writeErrorStream      : True
PSMessageDetails      : 
Exception             : Microsoft.ActiveDirectory.Management.ADException: An attempt was made to add an object to the directory with 
                    a name that is already in use ---> System.ServiceModel.FaultException: The supplied entry already exists.
                       --- End of inner exception stack trace ---
                       at Microsoft.ActiveDirectory.Management.AdwsConnection.ThrowExceptionForExtendedError(String 
                    extendedErrorMessage, Exception innerException)
                       at Microsoft.ActiveDirectory.Management.AdwsConnection.ThrowExceptionForFaultDetail(FaultDetail 
                    faultDetail, FaultException faultException)
                       at Microsoft.ActiveDirectory.Management.AdwsConnection.ThrowException(AdwsFault adwsFault, FaultException 
                    faultException)
                       at Microsoft.ActiveDirectory.Management.AdwsConnection.Create(ADAddRequest request)
                       at Microsoft.ActiveDirectory.Management.ADWebServiceStoreAccess.Microsoft.ActiveDirectory.Management.IADSy
                    ncOperations.Add(ADSessionHandle handle, ADAddRequest request)
                       at Microsoft.ActiveDirectory.Management.ADActiveObject.Create()
                       at Microsoft.ActiveDirectory.Management.Commands.ADNewCmdletBase`3.ProcessRecordOverride()
                       at Microsoft.ActiveDirectory.Management.Commands.ADCmdletBase.ProcessRecord()
TargetObject          : ou=Domain Controllers,DC=cryotest,DC=testdom
CategoryInfo          : NotSpecified: (ou=Domain Contr...test,DC=afcdom1:String) [New-ADObject], ADException
FullyQualifiedErrorId : An attempt was made to add an object to the directory with a name that is already in 
                    use,Microsoft.ActiveDirectory.Management.Commands.NewADObject
ErrorDetails          : 
InvocationInfo        : System.Management.Automation.InvocationInfo
ScriptStackTrace      : at Import-ADObjectOfClass, C:\Users\administrator\Desktop\Import-ADObjects.ps1: line 103
                    at <ScriptBlock>, C:\Users\administrator\Desktop\Import-ADObjects.ps1: line 137
                    at <ScriptBlock>, <No file>: line 1
PipelineIterationInfo : {1, 1}

如何在我的 Catch 块中使用更详细的信息?

4

3 回答 3

5

FullyQualifiedErrorId 只是异常对象的 .Message 属性以及异常产生的类的完全限定名称。

您无法通过FullyQualifiedErrorId 捕获,但您可以通过异常类型捕获:

try {
    # Do something that causes the 'name already in use' exception you're getting.
} catch [System.ActiveDirectory.Management.ADException] {
    if ($_.Exception.Message -ilike "*already in use") {
        # Do something to handle the error condition.
    }
}

请注意,这不是跨不同语言的可移植解决方案,因为异常消息可能在非英语版本的 Windows 上本地化。

此外,您可能必须修改您的 try 块以包含-ErrorAction Stop以确保捕获错误。

于 2013-08-08T22:23:53.807 回答
1

如果抛出的错误New-ADObject不是终止错误,则使用 catch 将无济于事。您可以做的一件事是使用 ErrorAction 参数使错误成为终止错误:

try{
   New-ADObject ... -ErrorAction Stop
}
catch{
   ... handle the error ....
}
于 2013-08-09T11:14:37.877 回答
0

我不知道您是否可以通过 捕获异常FullyQualifiedErrorId,但我找到了这种方法来获取它并且它对我有用:

$InerrMessage= $_.FullyQualifiedErrorId 
于 2020-12-14T17:35:09.763 回答