walshdj
asked on
Error Handing for a VBS script
Hi,
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.
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 "
chooseOUDirectory
Else
If strMbox = 2 Then
WScript.exit
End If
End If
End If
ASKER
Thanks for the comments. Here this i have just attached a code snippit. I do have on error resule at the top of the code.
I have been tuning on and off the on error resume next paramater at the top of the script so that I can troubleshoot the error. Eventually I will put it in the correct place in the code.
I have been tuning on and off the on error resume next paramater at the top of the script so that I can troubleshoot the error. Eventually I will put it in the correct place in the code.
Hi, you basically need to use
On Error Resume Next
and
Err.Clear
On Error GoTo 0
where ever you *expect* that an error might occur.
In general, what I do is use On Error Resume Next to continue if an error occurs, then I use
If Err.Number <> 0 Then
' an error occurred, handle this, then do the following
Err.Clear
On Error GoTo 0
Else
' No error occurred, so I usually turn the error checking back off, in case there is
' some other error that I am not expecting, which I want to know about
On Error GoTo 0
End If
So before your Execute statement, if you place On Error Resume Next, then follow my approach for the If statement after that, you should be handle an expected error. This will allow your program to behave as designed. The reason I use On Error GoTo 0 after those sections, is so that the script will tell you about any unexpected error (and crash out), which I want to know about, because it usually means I have a logic error somewhere.....
You can of course, just use On Error Resume Next at the top, and never use On Error GoTo 0, but you will need to use Err.Clear at expected error locations, to clear the Error status.
Regards,
Rob.
On Error Resume Next
and
Err.Clear
On Error GoTo 0
where ever you *expect* that an error might occur.
In general, what I do is use On Error Resume Next to continue if an error occurs, then I use
If Err.Number <> 0 Then
' an error occurred, handle this, then do the following
Err.Clear
On Error GoTo 0
Else
' No error occurred, so I usually turn the error checking back off, in case there is
' some other error that I am not expecting, which I want to know about
On Error GoTo 0
End If
So before your Execute statement, if you place On Error Resume Next, then follow my approach for the If statement after that, you should be handle an expected error. This will allow your program to behave as designed. The reason I use On Error GoTo 0 after those sections, is so that the script will tell you about any unexpected error (and crash out), which I want to know about, because it usually means I have a logic error somewhere.....
You can of course, just use On Error Resume Next at the top, and never use On Error GoTo 0, but you will need to use Err.Clear at expected error locations, to clear the Error status.
Regards,
Rob.
ASKER
Thanks Rob. I understand what you are saying but my issue is that If Err.Number <> 0 Then isnt picking up my error. It simply continues through the program and exit as normal. It is as f the error isnt being flagged as an error. Any ideas?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks Rob I've cracked it now thanks to your help.
BIt of a strange one (to me anyway) maybe you can explain. I had been using one generic "on Error resume next" command at the very top of the script to ignore or flag the errors. When I do this it doesnt pick up an error message (i.e if I use your msgbox idea or if I try to echo it out to the console) If I however put the "on Error resume next" command right before the code "expected" to fail it picks it up. I was planning to put it there anyway when I steped through all the bugs so that the code was neat and clean but it was easier to comment in and out the top line while debugging. Is there some reason that this does not work?
BIt of a strange one (to me anyway) maybe you can explain. I had been using one generic "on Error resume next" command at the very top of the script to ignore or flag the errors. When I do this it doesnt pick up an error message (i.e if I use your msgbox idea or if I try to echo it out to the console) If I however put the "on Error resume next" command right before the code "expected" to fail it picks it up. I was planning to put it there anyway when I steped through all the bugs so that the code was neat and clean but it was easier to comment in and out the top line while debugging. Is there some reason that this does not work?
Hi, well that certainly does seem odd. If an error is raised, the error number should be able to be displayed....perhaps an error was being raised earlier, and the expected code never executed for some reason....I guess you'd need to throw in some more MsgBox's to show output so you can figure out where it gets up to.
It's hard to say without being able to test your code in your environment, but you may be able to nut it out, or just keep the Error statements around the possible problem areas.
Regards,
Rob.
It's hard to say without being able to test your code in your environment, but you may be able to nut it out, or just keep the Error statements around the possible problem areas.
Regards,
Rob.
You need to have an On Error Resume Next at the beginning in order for error handling to work correctly. Otherwise it will just stop at the first error, as you're experiencing.
See this article for more information.