How Do I Catch by FullyQualifiedErrorId in PowerShell?

powershell, powershell-3.0

Solution

The FullyQualifiedErrorId is just the the .Message property of the exception object along with the the fully-qualified name of the class where the exception originated.

You can't catch by FullyQualifiedErrorId, but you can catch by exception type:

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.
    }
}

Note that this won't be a portable solution across different languages, since the exception message may be localized on non-English builds of Windows.

In addition, you may have to modify your try block to include `-ErrorAction Stop` to ensure the error is caught.

Problem

I have a script that creates new AD Objects (via New-ADObject, as it happens). If the object already exists, I need to catch and handle that. However, the exception type isn't nearly as explicit as the FullyQualifiedErrorId. Observe the below: ``` > $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} ``` How can I make use of the more verbose information here in my Catch block?

Original source