Solved

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

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

"Migrate" an SMTP relay receive connector to a new server using info from an old server.
This article will help you understand what HashTables are and how to use them in PowerShell.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
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…

708 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

13 Experts available now in Live!

Get 1:1 Help Now