Microsoft® Windows® 2000 Scripting Guide
« Previous | Next »
To run a single script against multiple computers you can include each computer name as a command-line argument. For example, this command runs the script Monitor.vbs against Server1, Server2, and Server3:
Cscript Monitor.vbs Server1 Server2 Server3
This works fine for a script that runs against two or three computers, but it is far less effective for scripts that need to run against scores of computers. For scripts that must run against more than a handful of computers, you will likely find it much more efficient to store the list of computer names in a text file; your script can then open the text file, read in each computer name, and then run against each of these computers. Not only is this efficient, but your text file need be no more complicated than this:
atl-dc-01 atl-dc-02 atl-dc-03 atl-dc-04
You can read arguments into a script by using the FileSystemObject. In the script shown in Listing 17.1, the FileSystemObject is used to read a list of server names from a text file; each name is then stored as a key-item pair with a Dictionary. This demonstration script then successively runs against each name in the Dictionary, connecting to the computer and reporting the number of services installed on that computer.
Note
Listing 17.1 contains a script that retrieves arguments from a text file. To carry out this task, the script must perform the following steps:
This constant will be used to open the text file in read-only mode. Opening the file in read-only mode ensures that the script cannot inadvertently overwrite the contents of that file.
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 C:\Scripts\Servers.txt, and the file is opened in read-only mode.
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. For more information about Dictionary keys and items, see "Script Runtime Primer" in this book.
The counter is initially set to 0 because, in VBScript, the first element in an array is element 0. Although any value can be used as a Dictionary key, setting the first element to 0 gets you used to the notion of working with 0 as the first number instead of 1.
You can identify the end of the text file by looping until the property AtEndOfStream is True. When this property is True, that means the entire file has been read. The script will then automatically exit the loop.
Because each line of the text file consists of a server name, strNextLine will contain the name of a server. If the first line in the text file is atl-dc-01, the value of strNextLine will also be atl-dc-01.
atl-dc-01 atl-dc-02 atl-dc-03 atl-dc-04
In that case, the Dictionary will consist of the following key-item pairs:
0 atl-dc-01 1 atl-dc-02 2 atl-dc-03 3 atl-dc-04
This query returns a collection consisting of all the services installed on the computer.
Listing 17.1 Retrieving Arguments from a Text File
|
|
Send us your feedback | « Previous | Next » |