Error Handling

Microsoft® Windows® 2000 Scripting Guide

The script in Listing 2.13 has a potential flaw in it. Suppose the user enters an invalid server name as an argument. When the script attempts to connect to this nonexistent computer, it will fail with the error message, "The remote server machine does not exist or is unavailable."

Of course, flaws like this are inherent in all the scripts used so far in this chapter, including those in which the computer names are hard-coded. After all, the script cannot distinguish between an invalid name and a valid name for a computer that, for some reason, is not available over the network. For example, suppose you run the script in Listing 2.12, and the computer named atl-dc-01 is offline. At that point, the script fails. In turn, you fail to retrieve the free disk space not only on atl-dc-01 but also on atl-dc-02 and atl-dc-03, even if those computers are connected to the network and functioning properly. This occurs because the script will stop running before it even attempts to connect to atl-dc-02 or atl-dc-03.

The inability to connect to atl-dc-01 is an example of a run-time error, an error that occurs after the script has started. (By comparison, a syntax error, such as a misspelled command, is generated and the script stops running before any lines of code are actually processed.) To help protect against run-time errors, you can include the VBScript error-handling mechanism On Error Resume Next in your scripts.

Without error handling, a script stops immediately upon encountering a run-time error. With error handling, a script does not stop but instead attempts to run the next line. The script proceeds by skipping the lines that generate errors and running lines that do not.