powershell - 如何在 PowerShell 中通过 FullyQualifiedErrorId 进行捕获?

标签 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}

如何在我的 Catch block 中使用这里更详细的信息?

最佳答案

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 block 以包含 -ErrorAction Stop 以确保捕获到错误。

关于powershell - 如何在 PowerShell 中通过 FullyQualifiedErrorId 进行捕获?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18136962/

相关文章:

c# - 如何克隆 Collection<T>?

c# - 如何正确关闭 ODP.net 连接 : dispose() or close()?

powershell - 发送请求时在 PowerShell 中使用括号

powershell - 隐藏 Invoke-WebRequest 的进度

powershell - 如何将默认凭据传递给 PowerShell Web 客户端对象? PS v2 的 Invoke-WebRequest 的替代方案?

xml - 将节点导入到另一个XML文档

powershell - 强制在 powershell 脚本中立即求值(按值复制变量,而不是引用)

powershell - Amazon Web Service PowerShell 凭据设置错误

performance - 为什么这个 PowerShell 代码 (Invoke-WebRequest/getElementsByTagName) 在我的机器上如此缓慢,但在其他机器上却没有?

powershell - Format-Table 忘记了一些属性,但 Format-List 显示了所有属性。为什么?