?
Solved

VBScript update IP Address of Secondary DNS Server

Posted on 2011-09-25
4
Medium Priority
?
661 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 2000 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

Percona Live Europe 2017 | Sep 25 - 27, 2017

The Percona Live Open Source Database Conference Europe 2017 is the premier event for the diverse and active European open source database community, as well as businesses that develop and use open source database software.

Question has a verified solution.

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

This program is used to assist in finding and resolving common problems with wireless connections.
This article is in regards to the Cisco QSFP-4SFP10G-CU1M cables, which are designed to uplink/downlink 40GB ports to 10GB SFP ports. I recently experienced this and found very little configuration documentation on how these are supposed to be confi…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Suggested Courses

764 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