Solved

VBScript update IP Address of Secondary DNS Server

Posted on 2011-09-25
4
627 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
IP address incorrect 8 63
voice vlan on meraki using 3CX 3 58
Need Help setting up a Virtual Lan on existing network for Test Domain 10 56
VIRTUAL NETWORKING 3 30
If your business is like most, chances are you still need to maintain a fax infrastructure for your staff. It’s hard to believe that a communication technology that was thriving in the mid-80s could still be an essential part of your team’s modern I…
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

911 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

23 Experts available now in Live!

Get 1:1 Help Now