Coding

VBScript to list and change DNS settings on all domain computers

If you ever change your Domain Controllers / DNS servers, you need to ensure all clients begin accessing the new servers before the old are retired. For DHCP, this is easy by editing the DHCP scopes and waiting until all the clients have renewed. For staticly assigned IPs, this can become a problem.

Below is a script that searches AD for all domain computers and queries them via WMI for DNS settings on each NIC. If the SetDNSServerSearchOrder line is uncommented, it will reset the DNS configuration of the systems.

'QueryDNS.vbs

Const ADS_SCOPE_SUBTREE = 2
arrNewDNSServerSearchOrder = Array("10.10.52.14","10.10.52.15")

EnumComputers

SUB EnumComputers
    on error resume next
    strDomain = "mydomain.com"

    Set objConnection = CreateObject("ADODB.Connection")
    Set objCommand =   CreateObject("ADODB.Command")
    objConnection.Provider = "ADsDSOObject"
    objConnection.Open "Active Directory Provider"

    Set objCOmmand.ActiveConnection = objConnection
    objCommand.CommandText = "Select Name, Location, whenChanged from 'LDAP://" & strDomain & "' Where objectCategory='computer'" 
    objCommand.Properties("Page Size") = 1000
    objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
    Set objRecordSet = objCommand.Execute
    objRecordSet.MoveFirst

    Do Until objRecordSet.EOF
        dLastChanged = objRecordSet.Fields("whenChanged").Value
        serverName = objRecordSet.Fields("Name").Value
        if dLastChanged>now()-60 THEN
            QueryDNS serverName
        END IF
            objRecordSet.MoveNext
    Loop
END SUB

SUB QueryDNS(strServerName)
    on error resume next
    Set objWMIService =    GetObject("winmgmts:{impersonationLevel=impersonate}!\" & strServerName & "rootcimv2")
    'Set colNICConfigs = objWMIService.ExecQuery("SELECT DNSServerSearchOrder, Description FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True AND DHCPEnabled = false")
    Set colNICConfigs = objWMIService.ExecQuery("SELECT    DNSServerSearchOrder, Description FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
    for each objNICConfig in colNICConfigs
        OldDNSConfiguration = Join(objNICConfig.DNSServerSearchOrder, ",")
        if LEN(OldDNSConfiguration)>1 THEN
            wscript.echo strServerName &","& OldDNSConfiguration
            'objNICConfig.SetDNSServerSearchOrder(arrNewDNSServerSearchOrder)    ' Uncomment to reset DNS servers
        end if
    next
END SUB

2 thoughts on “VBScript to list and change DNS settings on all domain computers

Leave a Reply