Wednesday, September 15, 2010

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 & "\root\cimv2")
    '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 comments:

Fred Castillo said...

I'm so glad I thought to search for this before trying to try and code it myself. You just saved me a few hours at least!

Fred | https://webhostinggeeks.com/tools/

Meiko Kaito said...

This is a topic of my interest. I love reading through your essay writer blog,I’ve read through a number of the articles in your website , and I love the way you blog. I wanted to leave a little comment to support you and wish you a good continuation