Command-Line ArgumentsCommand-line arguments provide you with the ability to modify the execution of a script prior to running it. Just the Steps
In the Ping.vbs script, which you examined in Chapter 2, "Looping Through the Script," and which appears in the next code listing (minus the comments), you use the variable strMachines to hold the target of the ping command. To ping other computers on the network, you have to modify the values within the quotation marks ("loopback;127.0.0.1;localhost" in this instance). Note
Modifying the values might be an acceptable solution when you always ping the same computers, but when you want the flexibility of the normal command-line ping, a better script is clearly called forthe command-line argument. Ping.vbs strMachines = "loopback;127.0.0.1;localhost" aMachines = Split(strMachines, ";") For Each machine In aMachines Set objPing = GetObject("winmgmts:")._ ExecQuery("select * from Win32_PingStatus where address = '" _ & machine & "'") For Each objStatus In objPing If IsNull(objStatus.StatusCode) Or objStatus.StatusCode<>0 Then WScript.Echo("machine " & machine & " is not reachable") Else WScript.Echo("reply from " & machine) End If Next Next Making the ChangeTo modify the Ping.vbs script to accept multiple computer names prior to running, you need to make two modifications:
That's all you need to do. The new script, named PingMultipleComputers.vbs, is shown here: PingMultipleComputers.vbs strMachines = WScript.Arguments.Item(0) aMachines = Split(strMachines, ";") For Each machine In aMachines Set objPing = GetObject("winmgmts:")._ ExecQuery("select * from Win32_PingStatus where address = '"_ & machine & "'") For Each objStatus In objPing If IsNull(objStatus.StatusCode) Or objStatus.StatusCode<>0 Then WScript.Echo("machine " & machine & " is not reachable") Else WScript.Echo("reply from " & machine) End If Next Next Running from the Command PromptTo run the script, you go to the command prompt in the directory containing your script and type the following: Cscript pingMultipleComputers.vbs localHost;127.0.0.1;loopback You use this syntax because of the Split function you used on the second line, which expects a ";" to separate the computer names. If you change the ";" on the second line into a ";" as seen in the next code line, you can use the comma character to separate the machine names and have a slightly more orthodox command. aMachines = Split(strMachines, ",") Once this modification is made, the command-line syntax looks like the following: Cscript pingMultipleComputers.vbs localHost,127.0.0.1,loopback
No Arguments?If a script tries to read command-line arguments not provided by the user, you get a Microsoft Visual Basic, Scripting Edition (VBScript) runtime error that makes a rather vague reference to "subscript out of range." This error is illustrated in Figure 4-1. Figure 4-1. When a Visual Basic script tries to read a command-line argument that was not supplied, you get a "subscript out of range" error message
If another administrator is running your script and gets the "subscript out of range" error, that administrator will have a hard time determining the cause of the message. A quick search at http://support.microsoft.com returns dozens of support articles referencing "subscript out of range," but none of them tell you that VBScript requires command-line arguments. It behooves you to make sure users of your Visual Basic scripts are not presented with such unfriendly error messages. Let's look at handling that now. Creating a Useful Error MessageWhen you supply command-line arguments for your scripts, the VBScript run time (called the Windows Scripting Host, or WSH for short) stores the arguments in an area of memory that is referenced by the WshArguments collection. This is nice because this storage location allows you to see how many command-line arguments are in there. Why is this important? It's important because when you know where the arguments are stored, and you know that they're kept in a collection, you can count the contents of that collection. For your script to run properly, there must be at least one argument supplied on the command line. You can make sure there is at least one argument by using the WScript.Arguments.Count method and putting it in an If...Then construction. To make the script easy to read, we place this logic in a subroutine. We call the subroutine first thing. If the value of the count is equal to zero, use WScript.Echo to send a message to the user that at least one argument is required. Once you make these modifications, CheckArgsPingMultipleComputers.vbs looks like the following: CheckArgsPingMultipleComputers.vbs Option Explicit On Error Resume Next Dim strMachines Dim aMachines, machine Dim objPing, objStatus subCheckArgs 'uses the count method for WshArguments strMachines = WScript.Arguments.Item(0) aMachines = Split(strMachines, ";") For Each machine in aMachines Set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}")._ ExecQuery("select * from Win32_PingStatus where address = '"_ & machine & "'") For Each objStatus in objPing If IsNull(objStatus.StatusCode) or objStatus.StatusCode<>0 Then WScript.Echo("machine " & machine & " is not reachable") Else WScript.Echo("reply from " & machine) End If Next Next Sub subCheckArgs If WScript.Arguments.count = 0 Then WScript.Echo "You must enter a computer to ping" & VbCrLf & _ "Try this: Cscript CheckArgsPingMultipleComputers.vbs " _ & "127.0.0.1;localhost" WScript.Quit End If End sub
Note
Supply value for missing argument
Note
![]() |