Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Vb script to change Static DNS on a list of servers in txt file

Posted on 2010-11-17
2
Medium Priority
?
1,043 Views
Last Modified: 2012-05-10
Hi,
I need a Vb script or a batch file that will check the server name from a txt file and then change the Preferred DNS and Alternative DNS to new IPs.

I got the script below but, running it doesn’t change DNS or even give errors.
We have a matrix of 2000, 2003 and 2008 servers will be in the txt file
Any ideas?

On Error Resume Next
Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objShell : Set objShell = CreateObject("Wscrfipt.Shell")
Dim objFile : Set objFile = objFSO.OpenTextFile("c:\Computers.txt") 
counter = 1
Do While Not objFile.AtEndOfStream
	currentserver = objFile.ReadLine
	Set objWMIService = GetObject("winmgmts:"  & "{impersonationLevel=impersonate}!\\" & currentserver & "\root\cimv2")
	Set colNicConfigs = objWMIService.ExecQuery  ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
 
	For Each objNicConfig In colNicConfigs
		If Not IsNull(objNicConfig.DNSServerSearchOrder) Then
			For Each strDNSServer In objNicConfig.DNSServerSearchOrder
				If strDNSServer = "192.168.20.40" Then
					If counter = 1 Then
						'It's the Primary DNS Server
						objShell.Run "netsh -r " &currentserver &" interface ip delete dnsserver " &chr(34) &"local area connection" &Chr(34) &" 192.168.20.40",1,True
						objShell.Run "netsh -r " &currentserver &" interface ip set dns name=" &chr(34) &"local area connection" &Chr(34) &" static 192.168.20.42",1,True
					ElseIf counter = 2 Then
						'Its the Secondary DNS Server
						objShell.Run "netsh -r " &currentserver &" interface ip delete dnsserver " &chr(34) &"local area connection" &Chr(34) &" 192.168.20.41",1,True
						objShell.Run "netsh -r " &currentserver &" interface ip set dns name=" &chr(34) &"local area connection" &chr(34) &" 192.168.20.43 index=2",1,True				
					End If
				End If				
				counter = counter + 1
			Next
		End If
	Next
Loop

Open in new window

0
Comment
Question by:rov17
[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
2 Comments
 
LVL 8

Accepted Solution

by:
spinzr0 earned 2000 total points
ID: 34161830
You should just be able to do it like this
On Error Resume Next
Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objShell : Set objShell = CreateObject("Wscrfipt.Shell")
Dim objFile : Set objFile = objFSO.OpenTextFile("c:\Computers.txt") 
counter = 1
Do While Not objFile.AtEndOfStream
	currentserver = objFile.ReadLine
	Set objWMIService = GetObject("winmgmts:"  & "{impersonationLevel=impersonate}!\\" & currentserver & "\root\cimv2")
	Set colNicConfigs = objWMIService.ExecQuery  ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
 
	For Each objNicConfig In colNicConfigs
		If Not IsNull(objNicConfig.DNSServerSearchOrder) Then
	            arrDNSServers = Array("192.168.20.42", "192.168.20.43")
                    objNicConfig.SetDNSServerSearchOrder(arrDNSServers)
		End If
	Next
Loop

Open in new window

0
 
LVL 5

Author Closing Comment

by:rov17
ID: 34169527
Perfect !!
Thanks very much
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

722 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