Categories
Domain Migration

VBSCript : Force Automatic DNS config on DHCP enabled NIC

Useful for AD / DNS migrations, this script will scan a text file containing a list of PC names and will interrogate each one to see if it is DHCP enabled on each NIC. If DHCP is enabled it will delete any manually assigned DNS servers and configure the NIC to automatically get the DNS servers via DHCP.

‘ Script to scan PCs for network cards that use DHCP and remove
‘ all manually set DNS servers for those cards only

On Error Resume Next

Const ForReading = 1

Set objFSO = CreateObject(“Scripting.FileSystemObject”)
Set objTextFile = objFSO.OpenTextFile _
    (“hosts.txt”, ForReading)

Do Until objTextFile.AtEndOfStream
    strComputer = objTextFile.Readline
    Wscript.Echo “Processing ” & strComputer
Set objWMIService = GetObject(“winmgmts:” _
  & “{impersonationLevel=impersonate}!\\” & strComputer & “\root\cimv2”)
Set colNicConfigs = objWMIService.ExecQuery _
  (“SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True”)
 
For Each objNicConfig In colNicConfigs
    WScript.Echo VbCrLf & ”  Network Adapter ” & objNicConfig.Index & _
      VbCrLf & ”    ” & objNicConfig.Description & VbCrLf
    If objNicConfig.DHCPEnabled Then
                intSetDNSServers = _
                objNicConfig.SetDNSServerSearchOrder
    Else
        WScript.Echo “DHCP not enabled on this card”
    End If
Next
Loop

Leave a Reply

Your email address will not be published. Required fields are marked *