Monday, January 26, 2015

Network settings: only want to change a DNS server ip address on a client computer Remotely / No interface or NIC name is required

So a Domain Controller/DNS server went down and the plan was to actually retire that server anyways.

We wanted to change the DNS Setting very fast. (replace the primary dns server on a large number of servers with another ip address).
I found this very nice script and decided to ran this as a test on a few servers, but from 96 machines it worked on 48 only (apparently the ping function did not work very well on some of our servers).
So I asked my coworker Seba Bistakis to create a similar VBS script.

Now we also wanted to avoid potential issues like a server down, or wmi not responding, access denied. So I created a so called "wmiPing" script in batch:
You will need Unix/Linux sed port which you can locate on some previous posts or any port on Internet, but this is only to extract those that failed.
======================================================================
for /f %%i in (lista.txt) do wmic /node:%%i os get | find /i "Windows" || echo %%i>>Falla.txt
copy lista.txt refiltrado.txt /y
for /f %%i in (Falla.txt) do sed -i "/%%i/Id" refiltrado.txt
del sed*.
======================================================================
(if you prefer to remove them manually you can just remove the last loop to run sed).
So the file refiltrado.txt is the one which we know wmi is working, we will use the content of that file.

And my coworker created a script to be ran locally, which I modified a little bit to be run remotely,
dns_change.vbs:
======================================================================
'Author(s): Seba Bistakis & Edu Nakama
Set objShell = WScript.CreateObject("WScript.Shell")
strFilename = "lista.txt"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTS = objFSO.OpenTextFile(strFilename)
Set objFSO2 = CreateObject("Scripting.FileSystemObject")
outFile = "lista1-executed.txt"
objFSO2.DeleteFile(outFile)
Set objFile = objFSO2.CreateTextFile(outFile,ForAppending,True)

Do Until objTS.AtEndOfStream

On Error Resume Next

strComputer = objTS.ReadLine

wscript.echo "============================"

Set objWMIService = GetObject("winmgmts:" _
 & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

If Err Then
 wscript.echo strComputer & ";Error;" & err.number & ";"
 Set objWMIService = Nothing
End If
On Error Goto 0

If Not objWMIService Is Nothing Then
objFile.Writeline strComputer
 Set colNicConfigs = objWMIService.ExecQuery _
  ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
 For Each objNicConfig In colNicConfigs
  If Not IsNull(objNicConfig.DNSServerSearchOrder) Then
   dns = objNicConfig.DNSServerSearchOrder
   For i = 0 To UBound(dns)
    wscript.echo strComputer & ";DNS" & i+1 & ";" & dns(i) & ";"
    If dns(i) = "10.261.40.23" Then
     dns(i) = "10.261.171.25"
     wscript.echo strComputer & ";DNS" & i+1 & ";" & dns(i) & ";CHANGED;"
    End If
   Next
   objNicConfig.SetDNSServerSearchOrder(dns)
  End If
 Next
End If

Loop
objFile.Close
objTS.Close
======================================================================
As you can see you will need to enter a each server on after another (one each line) on a filed called "lista.txt"

Run the script as cscript dns_change.vbs > Results.csv

Result.csv is the direct output of the script. As it is formated in CSV it can be opened on excel.
Lista1-executed.txt is a list of servers where the script was able to run on. If one is missing is because it couldn't ran on it.

Worked on: Windows 2003, Windows 2008, Windows 2012 servers.


0 comments:

Post a Comment