Tell Me Your NameOne 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 ArgumentsDespite 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 Making the Change to Named ArgumentsTo 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 ArgumentsTo 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
Check and supply named arguments
![]() |