Retrieving Arguments

Microsoft® Windows® 2000 Scripting Guide

In an enterprise setting, it is common to have a generic script that is designed to run at different times against different computers. For example, you might have a script that backs up and then clears the event logs on a computer. You might run this script every Monday against your domain controllers, every Tuesday against your Dynamic Host Configuration Protocol (DHCP) servers, and every Wednesday against your print servers.

To carry out this task, you might create separate scripts, one for the domain controllers, one for the DHCP servers, and so on. On Monday, you run the domain controller script, on Tuesday you run the DHCP script (which is identical to the domain controllers script except for the set of computers it runs against), and so on.

This works fine as long as you do not have to modify the script. However, what happens if you decide to have the script extract selected records to a text file before backing up and clearing the event logs? In that case, you will need to modify each version of the script, a process that is both time consuming and error prone.

Instead of creating separate scripts, each one with the computer names hard-coded, you might type in the appropriate command-line arguments each time you run a script. For example, to run the script against three mail servers, you could use a command similar to this:

cscript Backuplogs.vbs MailServer1 MailServer2 MailServer3

Assuming that your script has the code for parsing command-line arguments, this approach lets you run the script against a different set of computers any time you want.

Or at least you can do this as long as you have no more than two or three computers being passed as command-line arguments. But what if you need to run the script against 50 mail servers? In that case, you have to type in the names of all 50 computers anytime you run the script. What if you need to run the script against 85 print servers? Trying to pass all these computer names as command-line arguments will be extremely tedious, highly susceptible to typing mistakes, and likely exceed the maximum number of characters allowed in a command-line command.

Instead of typing command-line arguments, you can retrieve these arguments from a text file, a database, or even the Active Directory® directory service. By separating the arguments from the script, you can overcome the problems inherent in using command-line arguments: You do not have to worry about the time required to type 100 server names, you do not have to worry about typographical errors, and you do not have to be concerned with the maximum number of characters allowed in a command-line command.

In addition, this approach lets you add or delete computer names simply by editing the outside data source. Modifying a script requires someone with scripting knowledge and scripting experience; it is much easier to find someone capable of opening a text file and deleting the name of a computer recently decommissioned.

Using the Dictionary Object

Many of the sample scripts in this chapter work by retrieving arguments from an outside data source (for example, a text file or a database), storing those arguments in a Dictionary object, and then looping through the elements in the Dictionary and running the script by using each argument.

This is done primarily for the sake of efficiency. As an alternative, the scripts could instead:

  1. Connect to the outside data source.

  2. Retrieve the first argument.

  3. Run the script by using the first argument.

  4. Connect to the outside data source again, retrieve the next argument, and so on.

Although this approach works, it requires repeated calls to the data source. If the data source resides on a second computer, located across the network, the script will have to make continual calls across the network. This leaves the computer, and your script, prone to any number of network-related difficulties. For example, the remote computer where the data source resides could temporarily lose network connectivity, meaning that the script could no longer retrieve the computer names.

To avoid this issue, the scripts in this chapter make a single call to retrieve all the command-line arguments, and then store these arguments in a Dictionary object. The Dictionary, which is stored in local memory, is then used as the argument repository. (For more information about the Dictionary object, see "Script Runtime Primer" in this book.)