Previous Section Table of Contents Next Section

The WScript Object

All of these objects are accessed through the top-level WScript object. You've already seen WScript in use in Chapter 6, where I showed you how WScript.Echo can be used to produce both command-line output and message boxes, depending on whether you are using Cscript.exe or WScript.exe to execute your script. The WScript object is the only one your scripts get free, meaning you don't have to explicitly create a reference to it. WScript is always available when you're running a script in WSH.

In addition to Echo, the WScript object has new methods and properties that may be useful to you in your scripts. For example, you can execute the WScript.Sleep method, passing a specific number of milliseconds, to have your script pause its execution.


'Pause for 5 seconds

WScript.Sleep 300000

You can have your scripts immediately stop execution and exit, if you want.


If varInput = "" Then

 WScript.Quit

End If

In this example, the script will immediately exit if variable varInput is empty. You can also ensure that your scripts have a timeout. By default, WSH will continue executing your scripts forever; you may, however, want to automatically have your scripts end if they don't complete within, say, 30 seconds. That way, a script that has the chance of entering some endless loop, or trying to connect to a remote computer that isn't available, will eventually stop running. To do so, simply set a timeout value.


'Specify a timeout in seconds

WScript.Timeout = 30

Most importantly, the WScript top-level object provides access to important child objects that you'll need to use in many of your scripts.

    Previous Section Table of Contents Next Section