Changing the TCP/IP Settings

After your script can connect to Active Directory and return a recordset of computer names, you’re ready to use WMI to convert the machines from static IP addresses to DHCP-assigned addresses. You scrounge around and come up with a script that uses WMI to turn on DHCP. This script, called EnableDHCP.vbs, is shown here:

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

Get Microsoft® Windows® Scripting Self-Paced Learning Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.