Previous Page
Next Page

Using Multiple Arguments

In PingMultipleComputers.vbs, you use only one argument, which you assigned to the variable strMachines by using this command:

strMachines = WScript.Arguments.Item(0)

When you look at the command, you see that it's made up of several parts:

Variable

=

WScript.Arguments.Item

Item #

strMachines

=

WScript.Arguments.Item

(0)


If you need to use multiple arguments, you add another line and increment the item number contained within the parentheses.

Just the Steps

To implement multiple command-line arguments

1.
On a new line, assign a variable to WScript.Arguments.Item(0).

2.
On a new line, assign a variable to WScript.Arguments.Item(1).

3.
Use the variable from step 1 as you would any variable.

4.
Use the variable from step 2 as you would any variable.


Remember that the index values for the WScript.Arguments collection are zero-based, which means that the first item counted will be zero, as used in the PingMultipleComputers.vbs script. The following script (ArgComputerService.vbs) illustrates how you handle zero-based index values. In ArgComputerService.vbs, you use two arguments. The first one is a computer name, and the second argument is the name of a service. To run this script, change to the directory containing your script at a command prompt and use the following command:

Cscript argComputerService.vbs localhost lanmanserver

By using this command, the status of the lanmanserver server service on the localhost is returned to you. Lanmanserver is the name of the server service when it is registered in the registry. If you have access to a different machine, then supply the name or Internet Protocol (IP) address in place of localhost and use that name when running the following script, ArgComputerService.vbs.

ArgComputerService.vbs

Option Explicit
On Error Resume Next
Dim computerName
Dim serviceName
Dim wmiRoot
Dim wmiQuery
Dim objWMIService
Dim colServices
Dim oservice

computerName = WScript.Arguments(0)
serviceName = WScript.Arguments(1)
wmiRoot = "winmgmts:\\" & computerName & "\root\cimv2"
Set objWMIService = GetObject(wmiRoot)
wmiQuery = "Select state from Win32_Service" &_
   " where name = " & "'" & serviceName & "'"
Set colServices = objWMIService.ExecQuery _
  (wmiQuery)
For Each oservice In colServices
   WScript.Echo (serviceName) & " Is: "&_
   oservice.state & (" on: ") & computerName
Next

Header Information

The standard header information is in the ArgComputerService.vbs script. It begins with Option Explicit, which tells VBScript that you're going to specifically name all the variables you'll be using. If you fail to list a variable in this section, you get an error from VBScript. The variables used in ArgComputerService.vbs are listed in Table 4-1.

Table 4-1. Variables used in ArgComputerService.vbs

Variable Name

Use

computerName

Holds the first command-line argument

serviceName

Holds the second command-line argument

wmiRoot

Holds the namespace of WMI

wmiQuery

Holds the query issued to WMI

objWMIService

Holds the connection into WMI

colServices

Holds the result of the WMI query

oservice

Holds each service in colServices as you walk through the collection


Reference Information

In the Reference information section, you assign specific values to variable names to make the script work properly. By changing reference assignments, you can modify the script to perform other actions. The variable computerName is used to hold the first command-line argument. If the first item entered on the command line is not the name of a valid computer on the network, the script fails. In this particular script, you haven't taken steps to ensure the script will end normally. The variable serviceName is used to hold the value of the second item from the command line. In the same way that computerName must be the name of a valid computer on the network, serviceName must be the name of a valid installed service on the target computer. The service name is not the same as the display name that is used in the services application, rather it is the name assigned within the registry when the service is created. The script could be modified to provide a list of installed services on the target machine and then to allow the user to pick one of those services.

Tip

Select only information you intend to use from WMI. The wmiQuery variable used in ArgComputerService.vbs only selects the state of the service. The name is automatically selected and does not have to appear in the select statement here. If we had used Select *, then we would have returned all 25 properties of the service...a real waste when we are only using two of the properties in the Output section of the script.


Worker and Output Information

Once again, the Worker and Output information section of the script is quite simple:

For Each oservice In colServices
  WScript.Echo (serviceName) & " Is: " & _
  oservice.State & (" on: ") & computerName
Next

Because WMI returns service information in a collection (even when the collection has only a single value), you must use a For Each...Next loop to walk through each item in the collection to obtain your information. A For Each...Next loop is the engine that drives your script. The variable colServices contains every service that was returned by wmiQuery. The variable oservice holds each individual service and is used as the "hook" for asking for certain information through WMI. In this instance, you're interested only in the status information, and so you echo out the oservice.status information. If you modified the query contained in the wmiQuery variable, you'd be able to echo any of the information that is held within the Win32_Service part of WMI.

To find out more information about Win32_Service, search in the WMI Platform SDK in the Resources directory of the CD, or on www.microsoft.com.

The only other interesting aspect of the Worker and Output information section of the script is the use of concatenation, which was talked about in Chapter 3, "Adding Intelligence." Notice how the ampersand character (&) is used to glue two parts of the output line together. The other use of the ampersand is in conjunction with the underscore character (_). The underscore character signals to VBScript that the line is continued onto the next line. The ampersand character is often used with the line continuation character because the underscore breaks up the long line, and the ampersand is used to glue pieces together. Because a line might be in parts anyway, the line continuation character is a convenient place for breaking the script. The continuation character is primarily used to make a script more readable (both on screen and on paper).

Earlier in this section, you learned that ArgComputerService.vbs requires two command-line arguments: The first must be the name of a target computer, and the second must be the name of a valid service on the target computer. How would the user of the ArgComputerService.vbs script know about this requirement? If the script failed, the user could open the script in Notepad to see which argument is required. A second solution might be to modify the script so that when it failed, it would echo the correct usage to the user. There is, however, a third choicethe use of named argumentswhich is the subject of the next section.


Previous Page
Next Page