Solved

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

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
list O365 distribution group member's UPN 2 14
Execute Remote Powershell command Using System account 3 21
VB script help 23 30
DNS/WINS in a domain 10 37
Synchronize a new Active Directory domain with an existing Office 365 tenant
In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
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…

685 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