"Dave Susemiehl" <Dave Susemiehl@xxxxxx> wrote in message
> Hi, I can muddle through scripting ok most of the time but I'm not sure
> to handle this.
> So here's my dilemma, I want to use a text file for the OU's and Groups to
> create in the OU's. I read them into an array ok so I have them available
> but obviously each OU contains multiple groups. If I just cycle through
> Array Creating the OU's I will quickly run into an already exists error.
> couldn't find a good way to do an if exists on OU's so I decided to try to
> get the OU and if it errors the script would create the ou. The nice
> GET line looks something like this:
> Set mkOU = GetObject("LDAP://OU=" & arrOU(0) & ",OU=parentOU," &
> ---This line gets the desired results
> And then do an error trap so that if the 80005000 error is encounter It
> creates the OU.
> Any help would be appreciated.
Use "On Error Resume Next" to suppress normal error handling (which aborts
the script). Then you can test the Err object for error number and
description. Use "On Error GoTo 0" to restore normal error handling (this
also clears the Err object). For example:
On Error Resume Next
Set mkOU = GetObject("LDAP://ou=" arrOU(0) & ",ou=parentOU," & DomainPath)
If (Err.Number <> 0) Then
' Error raised attempting to bind the OU.
' Display error information (optional).
Wscript.Echo "Error binding to OU " & arrOU(0)
Wscript.Echo "Error Number: " & Err.Number
Wscript.Echo "Description: " & Err.Description
Wscript.Echo "Source: " & Err.Source
On Error GoTo 0
' Create the OU.
On Error GoTo 0
' The OU must already exist
If you know the specific error number you want to trap, you can test for
that. However, Err.Number will be decimal, while 80005000 is hex, so you
will need to convert. Best to test and see what Err.Number returns (some are
negative). I find Err.Number is -2147016656 when I attempt to bind to a
non-existent OU. If I have an invalid character (invald DN format) I
get -2147016654. Might be best just to test if Err.Number is non-zero.
MVP Directory Services
Hilltop Lab - http://www.rlmueller.net