Tuesday, May 1, 2012

Update DNS Server Entries on Multiple Servers

Recently we had upgraded our Windows 2000 Domain Controllers with Windows 2008 Domain Controllers and had to replace the DNS Servers  listed in Network Properties across all the member servers running windows 2000, Windows 2003 & Windows 2008 Servers. I created the following script to replace DNS Entries.. Replace "a.b.c.d"  with the actual dns server IP's in the script, create a file "d:\DNS_Order\servers.txt" with the server names listed in it. I ran it from command line as below: test.txt will record the output from the script which lists the DNS Entries of each and every server before and after completing the replacement..

C:\ cscript dns_replace_Serversfile.vbs >test.txt

dns_replace_Serversfile.vbs
Const ForReading = 1
Set objNetwork = CreateObject("Wscript.Network")
'Open the File System Object to Feed the Script
Set objFSO = CreateObject("Scripting.FileSystemObject")

'This line needs to be customized for the servers.txt localtion text file
Set objTextFile = objFSO.OpenTextFile ("d:\DNS_Order\servers.txt", ForReading)

'Starts the process reading thru the input file line by line until EOF
Do Until objTextFile.AtEndOfStream
strComputer = objTextFile.ReadLine
Wscript.Echo ""
Wscript.Echo "********************** Gathering Data On **********************"
Wscript.Echo " Computer Name From TXT File : "& strComputer
Wscript.Echo "********************** Gathering Data On **********************"


On Error Resume Next


arrNewDNSServerSearchOrder = Array("a.b.c.d", "a.b.c.d1", "a.b.c.d2", "a.b.c.d3")

Set objWMIService = GetObject("winmgmts:" _
 & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colNicConfigs = objWMIService.ExecQuery _
 ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")

WScript.Echo VbCrLf & "Computer: " & strComputer

For Each objNicConfig In colNicConfigs
  WScript.Echo VbCrLf & "  Network Adapter " & objNicConfig.Index
  WScript.Echo "    DNS Server Search Order - Before:"
  If Not IsNull(objNicConfig.DNSServerSearchOrder) Then
    For Each strDNSServer In objNicConfig.DNSServerSearchOrder
      WScript.Echo "        " & strDNSServer
    Next
  End If
  intSetDNSServers = _
   objNicConfig.SetDNSServerSearchOrder(arrNewDNSServerSearchOrder)
  If intSetDNSServers = 0 Then
    WScript.Echo "    Replaced DNS server search order list."
  Else
    WScript.Echo "    Unable to replace DNS server search order list."
  End If
Next

WScript.Echo VbCrLf & String(80, "-")

Set colNicConfigs = objWMIService.ExecQuery _
 ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")

For Each objNicConfig In colNicConfigs
  WScript.Echo VbCrLf & "  Network Adapter " & objNicConfig.Index
  WScript.Echo "    DNS Server Search Order - After:"
  If Not IsNull(objNicConfig.DNSServerSearchOrder) Then
    For Each strDNSServer In objNicConfig.DNSServerSearchOrder
      WScript.Echo "        " & strDNSServer
    Next
  End If
Next
Loop