Solved

VBScript update IP Address of Secondary DNS Server

Posted on 2011-09-25
4
660 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

#Citrix #Citrix Netscaler #HTTP Compression #Load Balance
Most of the applications these days are on Cloud. Cloud is ubiquitous with many service providers in the market. Since it has many benefits such as cost reduction, software updates, remote access, disaster recovery and much more.
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…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

724 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