Solved

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

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

919 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

17 Experts available now in Live!

Get 1:1 Help Now