Solved

Error Handing for a VBS script

Posted on 2009-07-14
7
494 Views
Last Modified: 2012-05-07
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.
'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

Open in new window

0
Comment
Question by:walshdj
  • 3
  • 3
7 Comments
 
LVL 38

Expert Comment

by:Shift-3
ID: 24852021
I haven't read through the whole script, but I do see one thing right off the bat.

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.
0
 
LVL 4

Author Comment

by:walshdj
ID: 24852275
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.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 24855532
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.
0
 
LVL 4

Author Comment

by:walshdj
ID: 24855594
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?
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 250 total points
ID: 24858051
Hmmm, under this
      Set objComputerFile = objCommand.Execute

you could put
MsgBox "Err.Number is " & Err.Number

and see whether it is equal to zero, or something else...that might help....

Regards,

Rob.
0
 
LVL 4

Author Comment

by:walshdj
ID: 24859023
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?
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 24867086
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.
0

Join & Write a Comment

Companies that have implemented Microsoft’s Active Directory need to ensure that the Active Directory is configured and operating properly. If there are issues found and not resolved, it eventually leads the components to fail or stop working and fi…
Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
This tutorial will walk an individual through the process of configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now