Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Update DNS settings with before and after results sent to a text file

Status
Not open for further replies.

Yodelaheho

IS-IT--Management
Mar 17, 2005
3
0
0
US
Hello All,
I spent a few weeks creating and perfecting two scripts, one for DNS settings and one for WINS. Thought you all might like to have them. At the bottome is a detailed description about how the script works.
Have fun!


OPTION EXPLICIT
Const INPUT_FILE_NAME = "C:\DNSScript\ServerList.txt"
CONST OUTPUT_FILE_NAME = "C:\DNSScript\Results_UpdateDNSServerSettings.txt"
Const FOR_READING = 1

arrDNSServers = Array("10.1.1.1", "10.1.1.2", "10.1.1.3", "10.1.1.4")

DIM objFSO, objFile, strcomputers, arrComputers
DIM objnewFSO, objnewFile, objnewcomputerlist
DIM strcomputer, objPing, objStatus, objWMIService
DIM colitems, objItem, i, arrDNSServers

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(INPUT_FILE_NAME, FOR_READING)
strComputers = objFile.ReadAll
objFile.Close
arrComputers = Split(strComputers, vbCrLf)

Set objnewFSO = CreateObject("Scripting.FileSystemObject")
Set objnewFile = objFSO.CreateTextFile(OUTPUT_FILE_NAME, True)
Set objnewcomputerlist = objFSO.OpenTextFile(OUTPUT_FILE_NAME, 1)

For Each strComputer In arrComputers
Set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery("select Replysize from Win32_PingStatus where address = '" & strComputer & "'")
For Each objStatus in objPing
'wscript.echo objStatus.ReplySize
If IsNull(objStatus.ReplySize) Then
objnewFile.WriteLine "------------------------"
objnewFile.WriteLine "Could not connect to computer: " & strComputer
Else
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

objnewFile.WriteLine "------------------------"
objnewFile.WriteLine "------------------------"
objnewFile.WriteLine "Server: " & strComputer

Set colItems = objWMIService.ExecQuery ("Select * from Win32_NetworkAdapterConfiguration WHERE IPEnabled = TRUE")

For Each objItem in colItems
'On Error Resume Next
If Not IsNull(objItem.DNSServerSearchOrder) Then
For i = 0 To UBound(objItem.DNSServerSearchOrder)
objnewFile.WriteLine "DNS Server Search Order Before: " & objItem.DNSServerSearchOrder(i)
Next
End If

objItem.SetDNSServerSearchOrder(arrDNSServers)

Next
objnewFile.WriteLine "-------------------------------------------"
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery ("Select * from Win32_NetworkAdapterConfiguration WHERE IPEnabled = TRUE")
For Each objitem in colItems
If Not IsNull(objItem.DNSServerSearchOrder) Then
For i = 0 To UBound(objItem.DNSServerSearchOrder)
objnewFile.WriteLine "DNS Server Search Order After: " & objItem.DNSServerSearchOrder(i)
Next
End If
Next
End If
Next
Next

Wscript.Echo "DONE"
_____________________________________________________________________________________________________________________

SCRIPT EXPLANATION:

OPTION EXPLICIT
Const INPUT_FILE_NAME = "C:\<folder>\<ServerList.txt>" (This is the location of a text file with your servers listed in them–Put whatever path and document name you want to in here)
CONST OUTPUT_FILE_NAME = "C:\<folder>\<Results_UpdateDNSServerSettings.txt>" (This is the location of a text file where the results will be posted to–Put whatever path and document name you want to in here)
Const FOR_READING = 1

arrDNSServers = Array("X.X.X.X", "X.X.X.X", "X.X.X.X", "X.X.X.X") (Enter the IP Addresses of your DNS Servers)
DIM objFSO, objFile, strcomputers, arrComputers (DIM statement = Declares variables and allocates storage space)
DIM objnewFSO, objnewFile, objnewcomputerlist
DIM strcomputer, objPing, objStatus, objWMIService
DIM colitems, objItem, i, arrDNSServers

Set objFSO = CreateObject("Scripting.FileSystemObject") (Set statement = Assigns an object reference to a variable or property)
Set objFile = objFSO.OpenTextFile(INPUT_FILE_NAME, FOR_READING)
strComputers = objFile.ReadAll
objFile.Close
arrComputers = Split(strComputers, vbCrLf)

Set objnewFSO = CreateObject("Scripting.FileSystemObject")
Set objnewFile = objFSO.CreateTextFile(OUTPUT_FILE_NAME, True)
Set objnewcomputerlist = objFSO.OpenTextFile(OUTPUT_FILE_NAME, 1)

For Each strComputer In arrComputers (This is where the script starts to read the <ServerList.txt> file line-by-line and then creates the output file)
Set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery("select Replysize from Win32_PingStatus where address = '" & strComputer & "'")
For Each objStatus in objPing
'wscript.echo objStatus.ReplySize
If IsNull(objStatus.ReplySize) Then
objnewFile.WriteLine "------------------------" (If it cannot connect to the server, then it enters a separation line into the document and below that enters “Could not connect to computer: <ServerName>” so you know which servers in your list were not updated)
objnewFile.WriteLine "Could not connect to computer: " & strComputer
Else
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

objnewFile.WriteLine "------------------------" (Enters double separation lines into the document and below that enters “Server: <ServerName>”. The double lines make things easier to view in the final document)
objnewFile.WriteLine "------------------------"
objnewFile.WriteLine "Server: " & strComputer

Set colItems = objWMIService.ExecQuery ("Select * from Win32_NetworkAdapterConfiguration WHERE IPEnabled = TRUE") (Only checks the enabled adapters)

For Each objItem in colItems (This runs through each of the enabled adapters and updates the output document with "DNS Server Search Order Before: <IPAddress>". Each DNS server will be entered on a separate line) 'On Error Resume Next
If Not IsNull(objItem.DNSServerSearchOrder) Then
For i = 0 To UBound(objItem.DNSServerSearchOrder)
objnewFile.WriteLine "DNS Server Search Order Before: " & objItem.DNSServerSearchOrder(i)
Next
End If

objItem.SetDNSServerSearchOrder(arrDNSServers) (This sets the new DNS records on each enabled adapter)

Next
objnewFile.WriteLine "-------------------------------------------" (Enters a single separation line in the document, to make things easier to read) Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery ("Select * from Win32_NetworkAdapterConfiguration WHERE IPEnabled = TRUE") (Again, only checks the enabled adapters)
For Each objitem in colItems (This, again, runs through all of the enabled adapters and updates the output document with "DNS Server Search Order After: <IPAddress>". Each DNS server will be entered on a separate line)
If Not IsNull(objItem.DNSServerSearchOrder) Then
For i = 0 To UBound(objItem.DNSServerSearchOrder)
objnewFile.WriteLine "DNS Server Search Order After: " & objItem.DNSServerSearchOrder(i)
Next
End If
Next
End If
Next
Next

Wscript.Echo "DONE" (This will give you a pop up window that says “DONE” at the end to let you know the script is complete)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top