Previous Page
Next Page

One Step Further: Modifying ComputerRoles.vbs

In this lab, you'll modify ComputerRoles.vbs so that you can use it to turn on Dynamic Host Configuration Protocol (DHCP) on various workstations. This is the first script we use that calls a WMI method.

Scenario

Your company's network was set up by someone who really did not understand DHCP. In fact, the person who set up the network probably could not even spell DHCP, and as a result every workstation on the network is configured with a static IP address. This was bad enough when the network only had a hundred workstations, but the network has grown to more than three hundred workstations within the past couple of years. The Microsoft Excel spreadsheet that used to keep track of the mappings between computer names and IP addresses is woefully out of date, which in the past month alone has resulted in nearly 30 calls to the help desk that were traced back to addressing conflicts. To make matters worse, some of the helpful administrative assistants have learned to change the last octet in Transmission Control Protocol/Internet Protocol (TCP/IP) properties, which basically negates any hope of ever regaining a managed network. Your task, if you should choose to accept it, is to create a script (or scripts) that will do the following:

  • Use WMI to determine the computer's role on the network and to print out the name of the computer, the domain name (if it is a member of a domain), and the user that belongs to the computer

  • Use WMI to enable DHCP on all network adapters installed on the computer that use TCP/IP

Your research has revealed that you can use Win32_ComputerSystem WMI class to obtain the information required in the first part of the assignment.

Warning

Keep in mind, this script will change network settings on the machine that this script runs on. Also, when run, it will need administrator rights to make the configuration changes. If you do not wish to change your TCP/IP settings, then do not run this script on your machine.


Part A
1.
Open up the ComputerRoles.vbs file from \My Documents\Microsoft PressVB ScriptSBS\Ch03\OneStepFurther and save it as YourNameComputerRoles Solution.vbs.

2.
Comment out On Error Resume Next so that you will receive some meaningful feedback from the Windows Script Host (WSH) run time.

3.
Dim new variables to hold the following items:

  • strcomputerName

  • strDomainName

  • strUserName

4.
Modify wmiQuery so that it returns more than just DomainRole from Win32_ComputerSystem. Hint: Change DomainRole to a wildcard such as *. The original wmiQuery line is seen below:

wmiQuery = "Select DomainRole from Win32_ComputerSystem"

The new line looks like this:

"Select * from Win32_ComputerSystem"

5.
Because colComputers is a collection, you can't directly query it. You'll need to use For Each...Next to give yourself a single instance to work with. Therefore, the assignment of your new variables to actual items will take place inside the For Each...Next loop. Assign each of your new variables in the following manner:

  • strComputerName = objComputer.name

  • strDomainName = objComputer.Domain

  • strUserName = objComputer.UserName

6.
After the completion of the Select Case statement (End Select) but before the Next command at the bottom of the file, use WScript.Echo to return the four items required by the first part of the lab scenario. Use concatenation (by using the ampersand) to put the four variables on a single line. Those four items are declared as follows:

  • Dim strComputerRole

  • Dim strcomputerName

  • Dim strDomainName

  • Dim strUserName

7.
Save the file and run it.

8.
Modify the script so that each variable is returned on a separate line. Hint: Use the intrinsic constant vbCrLf and the ampersand to concatenate the line. It will look something like this:

strComputerRole & vbCrLf & strComputerName

9.
Save and run the file.

10.
Use WScript.Echo to add and run a complete message similar to the following:

WScript.Echo("all done")

11.
Save and run your script. If it does not run properly, compare it with \My Documents\Microsoft PressVBScriptSBS\ch03\StepByStep\ComputerRoles Solution.vbs.

Part B
1.
Open the YourNameComputerRolesSolution.vbs file and save it as YourNameEnableDHCPSolution.vbs.

2.
Comment out On Error Resume Next so that you will receive some meaningful feedback from the WSH run time.

3.
Dim new variables to hold the new items required for this script. Hint: You can rename the following items:

  • Dim colComputers

  • Dim objComputer

  • Dim strComputerRole

4.
The new variables are listed here:

  • colNetAdapters

  • objNetAdapter

  • DHCPEnabled

5.
Modify the wmiQuery so that it looks like the following:

wmiQuery = "Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE"

6.
Change the following Set statement:

Set colComputers = objWMIService.ExecQuery (wmiQuery)

Now, instead of using colComputers, the statement uses colNetAdapters. The line will look like the following:

Set colNetAdapters = objWMIService.ExecQuery (wmiQuery)

7.
Delete the Select Case construction. It begins with the following line:

Select Case objComputer.DomainRole

And it ends with End Select.

8.
You should now have the following:

For Each objComputer In colComputers
  WScript.Echo strComputerRole
Next

9.
Change the For Each line so that it reads as follows:

For Each objNetAdapter In colNetAdapters

10.
Assign DHCPEnabled to objNetAdapter.EnableDHCP(). You can do it with the following:

DHCPEnabled = objNetAdapter.EnableDHCP()

11.
Use If...Then...Else to decide whether the operation was successful. If DHCP is enabled, DHCPEnabled will be 0, and you want to use WScript.Echo to echo out that the DHCP is enabled. The code looks like the following:

If DHCPEnabled = 0 Then
  WScript.Echo "DHCP has been enabled."

12.
If DHCPEnabled is not set to 0, the procedure does not work. So you have your Else condition. It looks like the following:

Else
  WScript.Echo "DHCP could not be enabled."
End If

13.
Conclude the script by using the Next command to complete the If...Then...Next construction. You don't have to put in a closing echo command, because you're getting feedback from the DHCPEnabled commands.

14.
Save and run the script. Compare your script with the EnableDHCPSolution.vbs script in the \My Documents\Microsoft Press\VBScriptSBS\ch03\OneStepFurther folder.

Chapter 3 Quick Reference

To

Do This

Evaluate a condition using If...Then

Place the condition to be evaluated between the words If and Then

Evaluate one condition with two outcomes

Use If...Then...Else

End an If...Then...Else statement

Use End If

Use an intrinsic constant in a script

Type it into the code (it does not need to be declared, or otherwise defined)

Evaluate one condition with three outcomes

Use If...Then...ElseIf, or use Select Case

Evaluate one condition with four potential outcomes

Use Select Case



Previous Page
Next Page