Merging WMI and ADSINow that you know that both the ADSI script and the WMI script work as advertised, merging the two scripts is a rather easy task. By merging them, you will connect to Active Directory, perform a query of all computers in the mred OU, take the returned data into a record set, iterate through the record set, and enable DHCP on each workstation in the record set until you reach the end of the file. Along the way, echo out the results of the DHCP operation. The new script is called ADouWMIDHCP.vbs. You need to assign a computer name to the variable Target. You do this inside the While Not...Wend loop by using Target = oRecordSet.Fields("name"), because as you walk through the record set, Target holds the name you get back. The variable Target will contain each computer name retrieved from ADSI during the execution of the script. Each name will then be used as a target of a WMI query. The rest of the WMI script is placed inside the While Not...Wend loop without additional alteration. Combining the two scripts enables you to leverage two different technologies to simplify a seemingly daunting desktop management problem. The only required changes to the ADSI script involved declaring the variables used by WMI in the Worker information section of the script. To make it obvious which variables were added with the merger, I added all new variables to two lines in the Header information section of the script. Although the only requirement for doing this is to place a comma between the variable names, I do not normally use this technique unless I have many variables that need to be declared. ADouWMIDHCP.vbs Option Explicit 'On Error Resume Next Dim qQuery Dim oConnection Dim oCommand Dim oRecordSet Dim oDom Dim oProvider Dim oOU Dim Target, oWMIService, colNetAdapters, oNetAdapter, errEnable oProvider = "'LDAP://" oDom = "dc=nwtraders, dc=msft'" oOU = "ou=mred," qQuery = "Select Name from " & oProvider _ & oOU & oDom & "where objectClass='computer'" Set oConnection = CreateObject("ADODB.Connection") Set oCommand = CreateObject("ADODB.Command") oConnection.Open "Provider=ADsDSOObject;" oCommand.ActiveConnection = oConnection oCommand.CommandText = qQuery Set oRecordSet = oCommand.Execute While Not oRecordSet.EOF Target= oRecordSet.Fields("name") 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 oRecordSet.MoveNext Wend oConnection.Close
![]() |