Solved

VBScript update IP Address of Secondary DNS Server

Posted on 2011-09-25
4
617 Views
Last Modified: 2012-05-12
Experts,

Would anybody happen to know where I can look on information to do th following?  We have probably about 150 servers (VMs) that need to be updated.

Is it possible to have VB query the local computer (like IPConfig), and whenever it sees a secondary DNS server listed as x.x.x.x update it with a different IP?

Any information that can be provided on this is more than appreciated.
0
Comment
Question by:usslindstrom
  • 3
4 Comments
 
LVL 65

Expert Comment

by:RobSampson
ID: 36596759
Hi, you can ue the SetDNServerSearchOrder method as shown here:
http://blogs.technet.com/b/heyscriptingguy/archive/2005/04/25/how-can-i-configure-a-computer-to-use-a-dynamically-assigned-dns-server.aspx

First, you would need to query the DNSServerSearchOrder property to find the required DNS Server, then keep the other IPs in the array, and just update one.

Regards,

Rob.
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 500 total points
ID: 36596834
I decided to have a crack at writing the script for you.  I haven't tested this, so just test it with one or two test computers in computers.txt

Regards,

Rob.
Set objFSO = CreateObject("Scripting.FileSystemObject") 
Const ForReading = 1

strComputers = "Computers.txt"
strLog = "Log.csv"
strOldIP = "10.0.0.1"
strNewIP = "10.0.0.2"

Set objLog = objFSO.CreateTextFile(strLog, True)
objLog.WriteLine """Computer"",""Result"""

Set objFile = objFSO.OpenTextFile(strComputers, ForReading, False) 
While Not objFile.AtEndOfStream 
	strComputer = Trim(objFile.ReadLine)
	If strComputer <> "" Then
		If Ping(strComputer) = True Then
			On Error Resume Next
			Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 
			Set colNICs = objWMIService.ExecQuery("Select DNSServerSearchOrder From Win32_NetworkAdapterConfiguration Where IPEnabled = True")
			If Err.Number = 0 Then
				On Error Goto 0
				For Each objNIC In colNICs
					blnFound = False
					strAddresses = ""
					For Each strDNSServer In objNIC.DNSServerSearchOrder
						If strDNSServer = strOldIP Then
							strAddress = strNewIP
							blnFound = True
						Else
							strAddress = strDNSServer
						End If
						If strAddresses = "" Then
							strAddresses = strAddress
						Else
							strAddresses = strAddresses & "|" & strAddress
						End If
						If blnFound = True Then
							On Error Resume Next
							objNetCard.SetDNSServerSearchOrder(Split(strAddresses, "|"))
							If Err.Number = 0 Then
								objLog.WriteLine """" & strComputer & """,""Update successful"""
							Else
								objLog.WriteLine """" & strComputer & """,""Update failed. Error " & Err.Number & ": " & Err.Description & """"
							End If
							Err.Clear
							On Error Goto 0
						Else
							objLog.WriteLine """" & strComputer & """,""No update required"""
						End If
					Next
				Next
			Else
				objLog.WriteLine """" & strComputer & """,""WMI Error " & Err.Number & ": " & Err.Description & """"
				Err.Clear
				On Error Goto 0
			End If
		Else
			objLog.WriteLine """" & strComputer & """,""Offline"""
		End If 
	End If 
Wend 
objFile.Close
objLog.Close

WScript.Echo "Finished. Please see " & strLog

Function Ping(strComputer)
	Dim objShell, boolCode
	Set objShell = CreateObject("WScript.Shell")
	boolCode = objShell.Run("Ping -n 1 -w 300 " & strComputer, 0, True)
	If boolCode = 0 Then
		Ping = True
	Else
		Ping = False
	End If
End Function

Open in new window

0
 
LVL 5

Author Closing Comment

by:usslindstrom
ID: 36596941
Absolutely brilliant!  - Thanks for the ideas here.

I see where you were going with this, and I'm going to massage it to make it work in out environment.

Much appreciated!
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 36596948
No worries.  I hope it works out for you.

Regards,

Rob.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

If you're not part of the solution, you're part of the problem.   Tips on how to secure IoT devices, even the dumbest ones, so they can't be used as part of a DDoS botnet.  Use PRTG Network Monitor as one of the building blocks, to detect unusual…
ADCs have gained traction within the last decade, largely due to increased demand for legacy load balancing appliances to handle more advanced application delivery requirements and improve application performance.
Viewers will learn how to connect to a wireless network using the network security key. They will also learn how to access the IP address and DNS server for connections that must be done manually. After setting up a router, find the network security…
After creating this article (http://www.experts-exchange.com/articles/23699/Setup-Mikrotik-routers-with-OSPF.html), I decided to make a video (no audio) to show you how to configure the routers and run some trace routes and pings between the 7 sites…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now