I am trying to create a generica VBS script to pull information from the Active directory. the script takes in inputs based on what active directory OU you want to run it against and runs the query based on those inputs. The script works fine but I want to create some error handing in case the inputs are incorrectly formatted or unrecognised. basically if the incorrect OU is chosen, or there is an typo in the input I want the script to flag that and promot for input again. I have tried to do it using if err <>0 but it doesnt pick up the error as being flagged. I suspect the issue is that the command object contains the error but I am unsure as to how to read this error and respond approporatly. I hope this makes sense, let me know if you require further detail. Sorry, I am in the process of teaching myself VBS and I may not be explaining myself quite right.
In the example below I am passing incorrect parameters for the level3OU, Level2OU and LEVEL1OU variables. It thows the error "Provider: Table does not exist." as expected but the "If err.number <> 0 Then" code does not kick in and try to run the chooseOUDirectory subroutine again to prompt for the variables again.
'Target the Search against the specified OU
Const ADS_SCOPE_SUBTREE = 2
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCOmmand.ActiveConnection = objConnection
objCommand.CommandText = _
"Select Name, Location from 'LDAP://" & level3OU & level2OU & level1OU & parentOU & strDNSDomain & "' " _
& "Where objectCategory='computer'"
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
Set objComputerFile = objCommand.Execute
If err.number <> 0 Then
strMbox = MsgBox("Cannot connect to that OU please try again", 1, "Formatting Error")
If strMbox = 1 Then
WScript.echo "Failed to connect to "
If strMbox = 2 Then