Microsoft® Windows® 2000 Scripting Guide
« Previous | Next »
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)
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:
This constant will be used when the text file is opened in read-only mode.
The Dictionary object will be used to store server names as those names are read from 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.
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.
You can identify the end of the text file by looping until the property AtEndOfStream is True.
Because each line of the text file consists of a server name, strNextLine will contain the name of a server.
This query returns a collection consisting of all the services installed on the computer.
Listing 17.2 Using a Text File as a Command-Line Argument
|
|
Send us your feedback | « Previous | Next » |