Changing the TCP/IP SettingsAfter 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
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 Header InformationThe 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 InformationIn 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 InformationIn 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.
|