如何在PowerShell中捕获FullyQualifiedErrorId?

Toh*_*huw 3 powershell powershell-3.0

我有一个创建新 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}
Run Code Online (Sandbox Code Playgroud)

如何利用 Catch 块中更详细的信息?

mik*_*kol 6

FullQualifiedErrorId 只是异常对象的 .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.
    }
}
Run Code Online (Sandbox Code Playgroud)

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

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

  • 在 catch 块内,$_ 应该是对导致您进入 catch 块的异常的 ErrorRecord 的引用。这应该与引用 $error[0] 大致相同。 (2认同)