Using a Text File as a Command-Line Argument

Microsoft® Windows® 2000 Scripting Guide

It is not unusual to have a script that needs to run against a different set of computers at different times. For example, you might have a monitoring script that occasionally runs against your Domain Name System (DNS) servers. At other times, you might run this same script against your DHCP servers. To ensure that the script runs against the correct set of computers, you can have separate text files, one containing the list of DNS servers and the other containing the list of DHCP servers. In this case, simply having the script read in the contents of a particular text file will not suffice; after all, the name of the text file to be read might change each time the script is run.

One way to have the same script run against a different set of computers at different times is to use the appropriate text file name as a command-line argument. To run the script against your DNS servers, you pass the script the name of the text file (for example, Dns_servers.txt) that contains the list of DNS servers. To run the script against the DHCP servers, use a different text file name (for example, Dhcp_servers.txt) as the command-line argument. In either case, the script opens the text file, reads in the computer names, and then runs against each of those computers.

For example, if you are running a script under Cscript, you can include the appropriate text file name as part of the command string that starts the script:

cscript monitor.vbs dns_servers.txt

Alternatively, you can use My Computer or Windows Explorer to drag the icon for the text file onto the icon for the script file. Any time you drag and drop a file onto a script file, the script file uses the dropped file name as an argument.

To illustrate how this works, this two-line script snippet echoes the path name for any file that is dragged onto the script file in Windows Explorer. If you drag a file onto the icon for this script, a message box appears, echoing the path of the file name:

Set objArgs = Wscript.Arguments
Wscript.Echo objArgs(0)

To use a file name as an argument, you need to make two minor modifications to the script shown in Listing 17.1. First, you need to create an instance of the WSH Arguments collections; this requires a single line of code:

Set objArgs = WScript.Arguments

Second, you need to modify the portion of the script that actually opens the text file. In Listing 17.1, the file path is hard-coded into the script:

Set objTextFile = objFSO.OpenTextFile _
    ("c:\scripts\servers.txt", ForReading)

In the script shown in Listing 17.2, the file path is not hard-coded. Instead, the script opens the file supplied as the command-line argument:

Set objTextFile = objFSO.OpenTextFile(objArgs(0), ForReading)

Scripting Steps

Listing 17.2 contains a script that uses a text file as a command-line argument. To carry out this task, the script must perform the following steps:

  1. Create an instance of the Wscript Arguments collection.

  2. Create a constant ForReading, and set the value to 1.

    This constant will be used when the text file is opened in read-only mode.

  3. Create an instance of the Dictionary object.

    The Dictionary object will be used to store server names as those names are read from the text file.

  4. Create an instance of the FileSystemObject.

  5. Use the OpenTextFile method to open the text file.

    You must specify two parameters when opening a text file: the path to the file and the mode in which to open the file. In this script, the path is not hard-coded (for example, C:\Scripts\Myservers.txt). Instead, the path is designated by the variable objArgs(0), which represents the first element in the arguments collection.

    This argument could have been supplied either by typing the file path at the command line or by using drag and drop to drop the icon for the text file on the icon for the script file. For example, if your script is named Monitor.vbs and you type the following command at the command prompt, objArgs(0) will be equal to C:\Scripts\ServerNames.txt:

    cscript Monitor.vbs C:\Scripts\ServerNames.txt

    As indicated by the second parameter, ForReading, the file is opened in read-only mode.

    If you want to use multiple text files as command-line arguments, you need to modify the script so that it loops through the entire Arguments collection. For more information on looping through the Arguments collection, see "WSH Primer" in this book.

  6. Set the counter variable i to 0.

    The counter variable will be used as the key to each element in the Dictionary. The name of the server will be used as the item associated with each key.

  7. Create a Do Until loop that will continue until each line of the text file has been read.

    You can identify the end of the text file by looping until the property AtEndOfStream is True.

  8. Use the ReadLine method to read the first line in the text file and store the value in strNextLine.

    Because each line of the text file consists of a server name, strNextLine will contain the name of a server.

  9. Use the Add method to add the counter variable i and the value of strNextLine to the Dictionary.

  10. Increment the value of i.

  11. Repeat the process with the next line in the text file.

  12. Set the value of the variable strComputer to the value of the first item in the Dictionary (for example, atl-dc-01). The variable strComputer will then represent the name of the first computer the script must connect to.

  13. Use a GetObject call to connect to the WMI namespace root\cimv2 on the remote computer (as specified by strComputer), and set the impersonation level to "impersonate."

  14. Use the ExecQuery method to query the Win32_Service class.

    This query returns a collection consisting of all the services installed on the computer.

  15. For each server the script connects to, echo the server name and the number of installed services.

  16. Repeat the process using each item in the Dictionary.

Listing 17.2 Using a Text File as a Command-Line Argument


Set objArgs = WScript.Arguments
Const ForReading = 1
Set objDictionary = CreateObject("Scripting.Dictionary")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile(objArgs(0), ForReading)
i = 0
Do While objTextFile.AtEndOfStream <> True
 strNextLine = objTextFile.Readline
 objDictionary.Add i, strNextLine
 i = i + 1
For Each objItem in objDictionary
 StrComputer = objDictionary.Item(objItem)
 Set objWMIService = GetObject("winmgmts:" _
 & "{impersonationLevel=impersonate}!\\" & strComputer& "\root\cimv2")
 Set colServices = objWMIService.ExecQuery _
 ("SELECT * FROM Win32_Service")
 Wscript.Echo strComputer, colServices.Count