Previous Page
Next Page

Tell Me Your Name

One of the rules I learned as a network administrator and as a consultant was to keep things simple. I'd therefore use short computer names and basic network designs as much as possible, because at some point, I'd be using ping.exe, tracert.exe, nslookup.exe, and so forth from the command line. As you know, I hate to type, so "the shorter the better" is my motto. This being the case, I am in somewhat of a quandary with this next section, because the methodology will require more typing on the command line.

Reasons for Named Arguments

Despite additional typing, there are valid reasons to use named arguments. One of the biggest reasons is the way VBScript handles unnamed arguments. For instance, in the ArgComputerService.vbs script, you must use command-line syntax such as this:

Cscript argComputerService.vbs computer1 lanmanserver

Suppose you happen to forget in which order the commands get entered, and you type the following:

Cscript argComputerService.vbs lanmanserver computer1

The script would fail unless you happen to have a server named lanmanserver on your network and a service named computer1 is running on lanmanserver. Don't laugh! I've seen stranger happenings. (For example, static Domain Name System [DNS] entries can point to the wrong machine. A ping would in fact workit would just go to the wrong computer. Those kind of errors are always fun.) Therefore, in keeping with my philosophy of trying to make things simple, let's explore how to create named arguments. You'll thank me, your boss will thank me, and even your mom will thank me (because stuff will run so well, and you'll be able to make it home for the holidays).

Named arguments can be used to make the order of command-line arguments irrelevant. This can make correct usage of running the script easier, especially when three or more distinct arguments are being used with a script that does not intuitively suggest a particular order.

Just the Steps

To implement named arguments

1.
On a new line, use the Set command to assign WScript.Arguments.Named to a variable.

2.
On the next line, create a name for the argument and assign it to a variable.

3.
On the next line, assign create a second name for the second argument and assign it to a variable as well.

4.
Use the variables defined in steps 2 and 3 as you would regular variables. Their values will be assigned when you run the script.


Making the Change to Named Arguments

To modify the previous script to require named arguments instead of unnamed arguments, you need to modify only four lines of code. The first change is to add an additional variable that will be used to hold the named arguments from the command line. The second modification will take place in the Reference section, in which you will assign the new variable to the named arguments collection. The last two changes will take place as you assign the variables to hold the server name and the service names in the script. The revised script, NamedArgCS.vbs follows:

NamedArgCS.vbs

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

Set colNamedArguments = WScript.Arguments.Named
computerName = colNamedArguments("computer")
serviceName = colNamedArguments("service")
wmiRoot = "winmgmts:\\" & computerName & "\root\cimv2"
wmiQuery = "Select state from Win32_Service" &_
   " where name = " & "'" & serviceName & "'"

Set objWMIService = GetObject(wmiRoot)
Set colServices = objWMIService.ExecQuery _
  (wmiQuery)
For Each oservice In colServices
   WScript.Echo serviceName & " Is: " &_
   oservice.state & " on: " & computerName
Next

The four lines that were changed in the preceding script are listed here:

Dim colNamedArguments
Set colNamedArguments = WScript.Arguments.Named
computerName = colNamedArguments("computer")
serviceName = colNamedArguments("service")

Because you added a variable for named arguments in the Reference section, you'll need to Dim that variable in the Header section. Declare colNamedArguments in the Header information section of the script. In the next line, you make colNamedArguments equal to the named arguments by using the Set command. You now assign each of the named arguments to variables of the same name: computerName and serviceName. This time, instead of simply referencing the WScript.Arguments element by index number, you are referencing the WScript.Arguments element using their names. Instead of simply using a 0 or a 1 (like we do when working with the unnamed arguments), you use the name from the command line.

Running a Script with Named Arguments

To supply data to a script with named arguments, you type the name of the script at the command prompt and use a forward slash (/) with the name of the argument you are providing, separated by a colon and the value you assign to the argument. The preceding script is named NamedArgCS.vbs, and it takes two arguments: computer and service. The command to launch this script is run against a computer named S2 and queries the lanmanserver service on that machine:

Cscript namedargcs.vbs /computer:127.0.0.1 /service:lanmanserver

Quick Check

Q.

What is one reason for using named arguments?

A.

With named arguments, when you have multiple command-line arguments, you don't need to remember in which order to type the arguments.

Q.

How do you run a script with named arguments?

A.

To run a script with named arguments, you use a forward slash and then enter the name of the argument. You follow this with a colon and the value you want to use.


Check and supply named arguments

1.
Open NamedArgCS.vbs from \My Documents\Microsoft Press\VBScriptSBS\ch04 in Notepad or your script editor of choice. Save it as YourNameCheckNamedArgCS.vbs.

2.
After you create the named arguments collection and assign arguments for both service and computer, call the subCheckArgs subroutine. To do this, place the name of the subroutine on a line by itself, as seen below:

subCheckArgs

3.
At the bottom of your script, begin the subroutine section with the word Sub followed by the name of the subroutine. This is seen below:

Sub subCheckArgs

4.
Skip down a few lines and end the subroutine section with the words End Sub.

5.
Now develop the logic that will check for the presence of two command-line arguments and supply a default value if one of the arguments is missing. Use an If...Then construction.

If colNamedArguments.Count < 2 Then

6.
Nest another If...Then block to test for the presence of the computer argument. Use the Exists method. If the argument does exist, this means that the service argument was omitted. Assign the spooler service to the serviceName variable and let the user know you are using the default service for the check. It will look like the following:

If colNamedArguments.exists("computer") Then
         serviceName = "spooler"
         WScript.Echo "using default service: spooler"

7.
If the service argument was supplied, and you are in this code block, then it means they omitted the computer argument. Assign the value localhost to the computerName variable and let the user know you are using the default computer. This code looks like the following:

Else If colNamedArguments.Exists("Service") Then
         computerName = "localHost"
         WScript.Echo "using default computer: localhost"

8.
If you are inside the If...Then statement, and it was not either the service or the computer that was missing, then it means the user completely munged the input to the command-line argument. Let's print out a friendly help message and end the script. This will look like the following:

Else
       WScript.Echo "you must supply two arguments" _
             & " to this script." & VbCrLf & "Try this: " _
             & "Cscript checkNamedArgCS.vbs /computer:" _
             & "localhost /service:spooler"
       WScript.Quit

9.
You will need three End If statements before End Sub.

10.
Save and run the script. Test each condition: no arugments, one argument, two arguments. The script should run fine. If it does not, then compare it with the CheckNamedArgCS.vbs script in the Chapter 4 folder.


Previous Page
Next Page