Solved

VBScript update IP Address of Secondary DNS Server

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
is there an export feature for easy reading in task scheduler 9 80
VBScript Issues 8 30
md5 password 3 61
Passing Credentials into a command line 13 32
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…
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
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…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

808 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