Solved

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

Posted on 2010-11-17
2
1,041 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 500 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
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…
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…

636 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