2

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?

3 Answers 3

5

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.

6
  • Can you clarify what object has the Exception.Message property set? It doesn't seem to exist on the pipeline object.
    – Tohuw
    Aug 9, 2013 at 13:27
  • 1
    Inside the catch block, $_ should be a reference to the ErrorRecord for the exception that caused you to enter the catch block. This should be roughly the same as referencing $error[0].
    – mikekol
    Aug 9, 2013 at 16:12
  • it seems It is not catching by that exception as all. Results continue as above. Any idea why? My catch block is Catch [Microsoft.ActiveDirectory.Management.ADException] {...}
    – Tohuw
    Aug 15, 2013 at 21:37
  • I'm not sure why that exception isn't being caught, assuming that it actually is of type [Microsoft...ADException]. You can cast a wider net and catch exceptions of any type if you don't specify a type in your catch block. That might be the best way to start - catch all types of exceptions, and then narrow it down to a specific type later on (or not).
    – mikekol
    Aug 15, 2013 at 22:13
  • That's a good idea, @mikekol. I threw it in and it revealed something surprising: the error isn't caught at all. I'll post details as an edit to my question, because the small comment blocks don't allow me to provide the full try catch I'm using.
    – Tohuw
    Aug 16, 2013 at 13:49
1

If the error thrown by New-ADObject is not a terminating error using catch will not help. One thing you can do is use the ErrorAction parameter to make the error a terminating error:

try{
   New-ADObject ... -ErrorAction Stop
}
catch{
   ... handle the error ....
}
2
  • "Object already exists" conditions can be caught; I'm already doing so for a different use case in which the exception is specific. I don't need to change the error action; try/catch works just fine for it there.
    – Tohuw
    Aug 9, 2013 at 13:08
  • However, you were right for this error. I don't understand why in some cases you need to change the ErrorAction and some you do not, but there you have it. Thanks!
    – Tohuw
    Aug 16, 2013 at 13:57
0

I don't know if you can catch the exceptions by the FullyQualifiedErrorId, but I found this way to get it and it works for me:

$InerrMessage= $_.FullyQualifiedErrorId 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.