Previous Page
Next Page

Using the For Each...Next Command Step-by-Step Exercises

In this section, you'll explore using the For Each...Next command and the For...Next command.

1.
Open up the ping.vbs script in Notepad. It is located in the My Documents\Microsoft Press\VBScriptSBS\Ch02\StepByStep folder.

2.
Change the values strMachines = "s1;s2" to one or more computers reachable on your network. (If you are not networked, you can do something like this: strMachines = "127.0.0.1;localhost;127.0.0.2").

3.
Save the script with a different name, such as YourNamePing.vbs.

4.
Open a CMD prompt and switch to the directory where you saved the script.

5.
Type cscript YourNamePing.vbs and see whether the script runs. If it does not, use the PING command from the CMD prompt to test your networked machine and ensure it is reachable. If you get a reply, make sure you have the quotation marks and the semicolon, as shown in step 2.

6.
Set Option Explicit.

7.
Dim each variable that is used in the script.

8.
Set On Error Resume Next, but comment it out.

9.
Add comments to identify each section of the script.

10.
Examine the construct of the For Each...Next statement.

11.
In the Worker and Output sections of the script, put in a For...Next statement that makes the script send three pings. Hint: Consider placing the For portion after the line that reads For each machine in aMachines.

12.
Save the script and test.

13.
If it runs properly, turn the On Error Resume Next statement back on by removing the comment.

14.
Save the script. If it does not run, compare it with pingSolution.vbs in the ch02\StepByStep folder.

15.
Extra: Play around with the script and see what optimizations you can add, such as reporting on different properties of the Ping command. Look up the WIN32_PingStatus WMI class in the Platform SDK for this information. Compare your results with pingSolutionExtra.vbs.

16.
Extra, Extra: Add additional comments to the script that explain why certain items are required.

17.
More Extras: Configure the script to ping a range of IP addresses (for testing, use 127.0.0.1127.0.0.255). Compare your results with pingSolutionMoreExtras.vbs.

18.
Even more: Have it ping only every fifth computer inside the range. Compare your results with pingSolutionEvenMore.vbs.

19.
More more extras: Configure the script to only return computers that do not respond to the ping. Compare your results with pingSolutionMoreMoreExtras.vbs.


Previous Page
Next Page