Retrieving Arguments from a Text File

Microsoft® Windows® 2000 Scripting Guide

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

  • This might not be the most practical use of an enterprise script; you rarely need to know how many services are installed on a computer. However, the actual activity performed by the script is irrelevant; the important aspect is how the script reads server names from a text file and then connects to each computer. A trivial task such as reporting the number of services installed was chosen simply to keep the focus on connecting to multiple computers and not on the activity carried out after each connection is made.

Scripting Steps

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:

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

    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.

  2. 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.

  3. Create an instance of the FileSystemObject.
  4. 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 C:\Scripts\Servers.txt, and the file is opened in read-only mode.

  5. 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. 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.

  6. Create a Do Until loop that continues 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. When this property is True, that means the entire file has been read. The script will then automatically exit the loop.

  7. 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. If the first line in the text file is atl-dc-01, the value of strNextLine will also be atl-dc-01.

  8. Use the Add method to add the counter variable i and the value of strNextLine to the Dictionary.
  9. Increment the value of i.
  10. Repeat the process with the next line in the text file. After the last line of the text file has been read, the Dictionary will consist of a set of items equivalent to the lines in that text file. For example, suppose the text file contains the following lines:
    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 
  11. 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.
  12. 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."
  13. Use the ExecQuery method to query the Win32_Service class.

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

  14. For each server the script connects to, use the Count property to echo the server name and the number of installed services.
  15. Repeat the process using each item in the Dictionary.

Listing 17.1   Retrieving Arguments from a Text File

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 
Const ForReading = 1 Set objDictionary = CreateObject("Scripting.Dictionary") Set objFSO = CreateObject("Scripting.FileSystemObject") Set objTextFile = objFSO.OpenTextFile _     ("c:\scripts\servers.txt", ForReading) i = 0 Do Until objTextFile.AtEndOfStream     strNextLine = objTextFile.Readline     objDictionary.Add i, strNextLine     i = i + 1 Loop 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 Next

send us your feedback Send us your feedback « Previous | Next »   


Microsoft Windows 2000 Scripting Guide(c) Automating System Administration 2003
Microsoft Windows 2000 Scripting Guide(c) Automating System Administration 2003
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 635

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net