Before you can work with the string of names that make up the sComputerName variable, you must put the names into an array. In their simplest and most common form, arrays are one-column tables of data, the rows of which are identified by index numbers from 0 through the final row numberthe upper bound of the array. You can create an array and populate it manually, or you can create an array and populate it from a chunk of existing data. To make the array from existing data, use the Split function to divide the sComputerName variable's content into chunks, using the carriage return/line feed (vbCrLf) as the delimiter. The code that converts the contents of sComputerName into the aComputers array looks like this:
aComputers = Split(sComputerName, vbCrLf)
Now that you have the array, you can use a For Each...Next statement on its contents to print each computer's name and display its services and their status.
Reading Computer Names from AD
To perform a complete inventory of the services running on all servers in AD (or a portion of AD), you can read the names directly from the domain controller (DC) instead of manually inputting the names or reading them from a text file. Connecting to the servers this way ensures that you inventory only present serversand avoids the potential problem of misspelling server namesbut results in a lot of output.
Obtaining the list of servers is easy: Connect to the Lightweight Directory Access Protocol (LDAP) service on the DC, retrieve the names of all computers in AD (or a portion of AD), and place those names into a collection. Next, run a For Each...Next statement against the collection to get each computer's common name (CN), then plug in that information to get the service status on each computer. For example, to query the contents of the Computers container, you'd use the code that Listing 5 shows. The rest of the For Each...Next statement enumerates the services on each computer and their status.
Output Redirection
Web Listing 1 (http:www.winnetmag.com, InstantDoc ID 41670) shows the assembled script, GetServiceStatus.vbs. Because this script can produce a lot of output, if you plan to run the script against more than one or two servers, I recommend that you don't try to read the output from the command window. Instead, redirect the output to a file, as follows:
getservicestatus.vbs [arguments]
> status.txt
Not only can you now check service status on local or remote computers, but you can also provide Help for your scripts, format script output, and work from a variety of input types. You'll probably find these abilities useful in other kinds of reporting scripts.
End of Article
Andrew Whitton May 14, 2004