Previous Page
Next Page

One Step Further: Combining WMI and ADSI in a Script

In this section, you combine the WMI script created in the previous exercise with an ADSI script. You can also use the \My Documents\Microsoft Press\VBScriptSBS\ch14\OneStepFurther\OSFch14Starter.vbs script.

Caution

If this script were to be run in a production environment, it would turn on DHCP on the targeted machines. This could interrupt network communications. Please use this script in a practice environment first, and make the appropriate changes before ever running it in a production environment.


1.
Open Notepad or your favorite script editor.

2.
Open the OSFch14Starter.vbs file.

3.
Open the \My Documents\Microsoft Press\VBScriptSBS\ch14\OneStepFurther\ConnectToADOU.vbs file.

4.
Save the ConnectToADOU.vbs file as YourNameConnectToADOU_DHCP.vbs.

5.
Copy the seven variable declarations from the OSFch14Starter.vbs file and paste them into the Header information section of your YourNameConnectToADOU_DHCP.vbs script. The seven variable declarations look like the following:

Dim target
Dim oWMIService
Dim colNetAdapters
Dim oNetAdapter
Dim DNSDomainErr
Dim DNSsearchErr
Dim DNSServer

6.
In your YourNameConnectToADOU_DHCP.vbs file, locate the While Not...Wend section of the script. Remove the WScript.Echo portion of the WScript.Echo oRecordSet.Fields("name") command.

7.
Replace the WScript.Echo command with Target = so that the new command looks like the following:

Target = oRecordSet.Fields("name")

8.
Copy the remaining portion of the OSFch14Starter.vbs script and paste it just below the new Target = oRecordSet.Fields("name") command. Make sure you do not include the target="." Section. The new While Not...Wend statement looks like the following:

While Not oRecordSet.EOF
    Target = oRecordSet.Fields("name")
    DNSserver=Array("128.1.2.1", "129.1.2.2")
    Set oWMIService = GetObject("winmgmts:\\" & target & "\root\cimv2")
    Set colNetAdapters = oWMIService.ExecQuery _
        ("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")
    For Each oNetAdapter In colNetAdapters
        DNSDomainErr = oNetAdapter.SetDNSDomain("NWTraders.com")
        DNSsearchErr=oNetAdapter.SetDNSServerSearchOrder(DNSserver)
        WScript.Echo "DNSDomain returned " & (DNSDomainErr)
        WScript.Echo "DNSsearchOrder returned " & (DNSsearchErr)
    Next
    oRecordSet.MoveNext
Wend

9.
Save your work.

10.
Test the script. If it works, remove the comment from the On Error Resume Next command. If it doesn't work, compare it with \My Documents\Microsoft Press\VBScriptSBS\ch14\OneStepFurther\ConnectToADOU_DHCP.vbs.

Chapter 14 Quick Reference

To

Do This

Control the behavior of NetBIOS over TCP/IP

Use the WIN32_NetworkAdapterConfiguration class

Disable NetBIOS over TCP/IP

Use the SetTcpIpNetios method of the WIN32_NetworkAdapterConfiguration class

Specify a unique domain name for a network connection

Use the SetDNSDomain method from the WIN32_NetworkAdapterConfiguration class

Specify a DNS server

Use the SetDNSServerSearchOrder method from the WIN32_NetworkAdapterConfiguration class

Obtain an up-to-date list of computers for performing WMI configuration operations

Use ADSI to query Active Directory for the computers; then call the appropriate WMI methods to perform the configuration



Previous Page
Next Page