Previous Page
Next Page

Changing the TCP/IP Settings

After your script can connect to Active Directory and return a record set of computer names, you're ready to use WMI to convert the machines from static Internet Protocol (IP) addresses to Dynamic Host Configuration Protocol (DHCP)assigned addresses. You scrounge around and come up with a script that uses WMI to turn on DHCP.

Caution

Please note this script will turn on DHCP on a computer. If you were to run it on a production server, it would turn on DHCP, and the server would request an IP address from your DHCP server. This could result in workstations not being able to communicate with your server. These scripts should be only used in a test environment until you configure them for your production environment.


The script to use to turn on DHCP, called EnableDHCP.vbs, is shown here:

EnableDHCP.vbs

Target = "."
Set oWMIService = GetObject("winmgmts:\\" & Target & "\root\cimv2")
Set colNetAdapters = oWMIService.ExecQuery _
 ("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")
For Each oNetAdapter In colNetAdapters
    errEnable = oNetAdapter.EnableDHCP()
     If errEnable = 0 Then
        WScript.Echo "DHCP has been enabled."
    Else
        WScript.Echo "DHCP could not be enabled."
    End If
Next

Just the Steps

To enable DHCP by using WMI

1.
Make a connection to WinMgmts on the target machine.

2.
Connect to the root\cimv2 namespace in WMI.

3.
Create a collection to hold the result of the query.

4.
Use a query to choose network adapters that have TCP/IP bound and enabled.

5.
Use a For Each...Next loop to iterate through the collection of network adapter configurations.

6.
Use the EnableDHCP command on each network adapter configuration.


Header Information

The Header information section in this script is similar to the ConnectToADOU.vbs script. The variables are Target, oWMIService, colNetAdapters, oNetAdapter, and errEnable. When you merge the WMI script with Active Directory Service Interfaces (ADSI) script such as the ones examined in Chapters 11 and 12, you will need to declare new variables such as the ones mentioned here.

Reference Information

In the Reference information section, you assign values to the variables used in the script. The variable oWMIService is assigned to the hook that comes back from WMI when you use the CreateObject command. You attach to the root\cimV2 namespace on the target machine. You use colNetAdapters to hold the hook that comes back from running a query against the WMI namespace. The query you run is designed to return all the network adapter configurations installed on the target computer that are IP-enabled. You do this because there is no point in trying to turn on DHCP on an Internetwork Packet Exchange/Sequenced Packet Exchange (IPX/SPX) or AppleTalk network adapter configuration.

Worker and Output Information

In the Worker information section of the script, you use the oNetAadapter variable as a placeholder by using the For Each...Loop to help you iterate through the collection of network adapter configurations. One nice thing you do here is use a variable called errEnable. You set errEnable to be equal to the value that is returned by VBScript when you try to turn on DHCP by using the enableDHCP command. If the operation is successful, the return code is 0. However, if the operation fails, you get a different return code. In this script, you're interested only in whether DHCP works. So if the return code is 0, everything is fine, and you echo out that DHCP was enabled. If DHCP enablement fails, you get a different error code as just mentioned, and so you use the Else part of the script and simply echo that DHCP failed.

Quick Check

Q.

To programmatically turn on DHCP, to which WMI namespace do you connect?

A.

You need to connect to root\cimV2.

Q.

In what fashion does WMI return the network adapter?

A.

WMI returns the network adapter as a collection.

Q.

What return code indicates a successful WMI operation?

A.

A return code of 0 indicates a successful WMI operation.



Previous Page
Next Page