O'Reilly logo

Active Directory, Second Edition by Alistair G. Lowe-Norris, Robbie Allen

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Checking for Errors in VBScript

It is worthwhile to look at error handling in a little more detail now. Normally errors that occur in a script are termed fatal errors. This means that execution of the script terminates whenever an error occurs. When this happens, a dialog box opens and gives you the unique number and description of the error. While this is useful, sometimes you may like to set errors to be nonfatal, so that execution continues after the error. To do this, you include the following line in your code:

On Error Resume Next

Once you have done this, any line with an error is ignored. This can cause confusion, as can be seen from the following code. Note the missing P in LDAP:

On Error Resume Next
   
Set objGroup = GetObject("LDA://cn=Managers,ou=Sales,dc=mycorp,dc=com")
   
objGroup.GetInfo
WScript.Echo objGroup.Description
objGroup.Description = "My new group description goes here"
objGroup.GetInfo
WScript.Echo objGroup.Description

This script fails to execute any of the lines after the On Error Resume Next statement, as the first LDAP call into the objGroup variable failed. However, it will not terminate as usual with an error after the GetObject line, due to the On Error statement. To get around this, you should add a couple lines to do error checking. Example 19-9 is a good example of error checking in a different script.

Example 19-9. Error checking in VBScript

On Error Resume Next '********************************************************************** 'Clear errors '********************************************************************** ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required