How to get data in and out of a WSH script
Let's suppose you develop a script that performs some action on a computer or user account. The script works great, and now you want to run the script for a different computer. The trouble is you hard-coded the target computer name in the script. As a result, you must edit the script before you run it for the new target computer. A better approach is to provide the target computer name to the script at runtime.
To provide the computer name at runtime, you need to understand the input and output facilities in Windows Scripting Host (WSH). This month, I examine the variety of mechanisms available in WSH for getting data in and out of a script.
Understanding how to work with external data in a script is important in numerous situations. Passing arguments to a script, prompting a user for data, displaying a message, reading an input file, and writing to a log file all require knowledge of WSH's input and output capabilities. First, I review the available input and output options. Then, I demonstrate how to effectively use input and output options in a script.
I/O Options
Table 1, page 186, lists the input and output options that WSH and the VBScript engine provide. Some input and output capabilities that Table 1 lists appear to overlap in functionality, but the overlap exists for a reason. Recall that Microsoft uses the components that make up the Windows scripting technology in a variety of products. For example, Microsoft includes VBScript with WSH, Microsoft Internet Explorer (IE), Internet Information Server (IIS), and Visual Studio (VS). Although the two mechanisms might appear to duplicate functionality, they provide distinct capabilities unique to the primary environment that each supports.
For example, consider WScript Shell's Popup method and VBScript's MsgBox function. WScript Shell's Popup method is best suited for automated scripts because the method supports a timeout parameter. If a user doesn't respond to a Popup-generated dialog box within a specified period of time, the method silently dismisses the dialog box and the script continues. Microsoft initially designed VBScript's MsgBox function to communicate with a user via IE, so MsgBox provides a mechanism to display a graphical message to a user and capture the button the user clicks to dismiss the message box. Popup requires you to create a Shell object before you use the method. MsgBox doesn't, so MsgBox is a little more efficient. If you don't need a timeout value, use MsgBox for efficiency. If you need a timeout value, use Popup.
Let's examine some input and output mechanisms in more detail. The goal of scripting is to produce scripts that are flexible, reusable, and can run unattended in an automated environment. Therefore, rather than cover the graphical forms of input and output, I'll concentrate on the features you can use in an automated environment: WScript's Arguments collection and the FileSystemObject's TextStream object.
WScript Arguments Collection
Arguments for a WSH script are accessible via the WScript object's Arguments property. Internally, the Arguments property references a collection. Understanding what a collection is and how to work with a collection are the keys to understanding and implementing WScript arguments.
A collection is a special data type that contains a set of related items. In some ways, you can think of a collection as an array with properties. The key difference is that unlike arrays, which use integer subscripts to access individual elements, collections are objects that expose several properties that you can use to access collection members. The properties are Item(n), Count, and Length. Item(n), where n is a member's location in the collection, returns an individual collection member. Count and Length perform the function of providing the total number of members in a collection. (The Length property isn't a standard collection property. According to the documentation, Microsoft includes the Length property on WSH collections for JScript compatibility.) Some collections also implement methods such as Add and Remove that let you add and remove collection members, but the Arguments collection doesn't implement these methods.
Figure 1, page 189, shows how the WScript object uses the Arguments property to expose the Arguments collection. Because every WSH script automatically creates a WScript object, the Arguments collection is immediately available and accessible using the WScript.Arguments property and the standard collection properties.
To access members in the Arguments collection, you can use dot (.) notation. (For information about using dot notation, see Scripting Solutions, "Scripting 101, Part 3," August 1999.) For example, let's say you have a script you've written in VBScript and you want to pass to the script the first two arguments that Figure 1 shows. You can access the arguments as follows:
strComputer = _
WScript.Arguments.Item(0)
strUser = _
WScript.Arguments.Item(1)
When a property is absent, the object accesses the default property. Because Item is the default property method of the collection, you can omit the Item property to shorten these arguments.
strComputer = _
WScript.Arguments(0)
strUser = WScript.Arguments(1)
A third way to access the Arguments collection is to set a reference to the WScript.Arguments property, then use the reference to access the collection members. Using a reference eliminates the need to type WScript.Arguments for each argument you want to access:
Set oArgs = WScript.Arguments
strComputer = oArgs(0)
strUser = oArgs(1)