Submitted By: Baron McEntire

Modifies the DNS servers assigned to client machines.

Visual Basic
Edit|Remove
On Error Resume Next

Const ForReading = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("c:\Scripts\computers\dns.txt", ForReading)

Do Until objTextFile.AtEndOfStream 
    strComputer = objTextFile.Readline

	strOldDNSServer1 = "192.168.2.1"
	strOldDNSServer2 = "192.168.2.2"
	strNewDNSServer1 = "192.168.1.1"
	strNewDNSServer2 = "192.168.1.2"

	 
	Set objWMIService = GetObject("winmgmts:" _
	& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
	
	Set colNicConfigs = objWMIService.ExecQuery _
	("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
	 
	WScript.Echo VbCrLf & "Computer: " & strComputer
	 
	For Each objNicConfig In colNicConfigs
		WScript.Echo VbCrLf & "  Network Adapter " & objNicConfig.Index
		arrDNSServerSearchOrder = objNicConfig.DNSServerSearchOrder
		WScript.Echo "    DNS Server Search Order - Before:"
		If Not IsNull(objNicConfig.DNSServerSearchOrder) Then
			For Each strDNSServer In objNicConfig.DNSServerSearchOrder
				WScript.Echo "        " & strDNSServer
			Next
		End If

		
		blnFound = 0
		For i = 0 to UBound(arrDNSServerSearchOrder)
			If arrDNSServerSearchOrder(i) = strOldDNSServer1 Then
				arrDNSServerSearchOrder(i) = strNewDNSServer1
				blnFound = 1
			End If				
			If arrDNSServerSearchOrder(i) = strOldDNSServer2 Then
				arrDNSServerSearchOrder(i) = strNewDNSServer2
				blnFound = 1				
			End If
		Next
	
		'===============================================================

		If blnFound Then
			retSetDNS = objNicConfig.SetDNSServerSearchOrder(arrDNSServerSearchOrder)
			If retSetDNS = 0 Then
				For i = 0 to UBound(arrDNSServerSearchOrder)
					If arrDNSServerSearchOrder(i) = strNewDNSServer1 Then
						WScript.Echo "    Replaced " & strOldDNSServer1 & " with " & arrDNSServerSearchOrder(i) & " in DNS search order."					
					End If				
					If arrDNSServerSearchOrder(i) = strOldDNSServer2 Then
						WScript.Echo "    Replaced " & strOldDNSServer2 & " with " & arrDNSServerSearchOrder(i) & " in DNS search order."								
					End If
				Next
			Else
				WScript.Echo "    Failed to update DNS server search order. Return Code = " & retSetDNS
			End If
		Else
			WScript.Echo "    BOTH DNS servers " & strOldDNSServer1 & " and " & strOldDNSServer2 & " were not found."
		End If
	Next
 
	WScript.Echo VbCrLf & String(80, "-")
	 
	Set colNicConfigs = objWMIService.ExecQuery _
		("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
	 
	For Each objNicConfig In colNicConfigs
		WScript.Echo VbCrLf & "  Network Adapter " & objNicConfig.Index
		WScript.Echo "    DNS Server Search Order - After:"
		If Not IsNull(objNicConfig.DNSServerSearchOrder) Then
			For Each strDNSServer In objNicConfig.DNSServerSearchOrder
				WScript.Echo "        " & strDNSServer
			Next
		End If
	Next

	' =====================================================================
	' End
	' =====================================================================

Loop ' GO GET Next line from file

objTextFile.Close