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.


arrNewDNSServerSearchOrder = Array("","")


SUB EnumComputers
    on error resume next
    strDomain = ""

    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

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

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

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

  1. 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

Leave a Reply

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