Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to change the DNS of a computer, according to network ?

Status
Not open for further replies.

dickyboy37

Technical User
Jul 10, 2006
7
FR
Hy From France,

I want to change the primary DNS and the secondary DNS of a computer like XP or Seven according to network like this :

If mynetwork is 192.168.1.0 255.255.252.0 then
arrDNSServers = Array("x.x.x.x", "y.y.y.y.")
elseif mynetwork is 192.168.4.0 255.255.252.0 then
arrDNSServers = Array("t.t.t.t", "v.v.v.v")
end if

Have you a idea ?
Thanks and sorry for my poor english ;-)

Gil.
 
here's a script I wrote in 2009 to do just that. Disect it to meet your needs.

Code:
'************************************************************************
'DESCRIPTION:   														
'WRITTEN BY:    Daniel M. Jones											
'DATE:			June 11, 2009											
'UPDATE:																
'************************************************************************

'on error resume next

'************************************************************************
' VARIABLE DEFINITION
'************************************************************************


CONST SELF_VERSION = "0.1"
CONST SELF_SCRIPT  = "Set DNS"
CONST UNKNOWN = "Chickenless Soup"

dim arrNewDNS(1)
dim arrOldDNS(1)

arrNewDNS(0) = "10.15.200.57"
arrNewDNS(1) = "10.15.200.60"
arrOldDNS(0) = "10.5.1.2"
arrOldDNS(1) = "10.15.200.31"

set objFSO   = WScript.CreateObject("Scripting.FileSystemObject")
set objReg   = GetObject("winmgmts:\\.\root\default:StdRegProv")
set objShell = WScript.CreateObject("WScript.Shell")

'************************************************************************
' FUNCTIONS
'************************************************************************

function ping (strComputer)
	ping = false
	set objExec = objShell.Exec("%comspec% /c ping " & strComputer & " -n 1 -w 100")
	do until objExec.Stdout.AtEndOfStream
		strLine = objExec.StdOut.ReadLine
		if (inStr(strLine, "Reply")) then ping = true
	loop
end function


'************************************************************************
' BEGIN
'************************************************************************

strComputer = ucase(inputBox("Asset"))

if (ping(strComputer) = false) then msgbox "Computer unreachable" : wscript.quit

set objWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
set tblNICs = objWMI.ExecQuery("Select DNSServerSearchOrder From Win32_NetworkAdapterConfiguration")

for each objNIC in tblNICs
	arrDNS = objNIC.DNSServerSearchOrder
	if (isArray(arrDNS)) then
		strCurrentDNS = join(arrDNS)
		for each strOldDNS in arrOldDNS
			if (inStr(strCurrentDNS, strOldDNS)) then strCurrentDNS = replace(strCurrentDNS, strOldDNS, "")
		next
		for each strNewDNS in arrNewDNS
			if (inStr(strCurrentDNS, strNewDNS) = 0) then strCurrentDNS = strCurrentDNS & " " & strNewDNS
		next
		strCurrentDNS = replace(trim(strCurrentDNS), " ", ",")
		arrCompleteDNS = split(strCurrentDNS, ",")
		wscript.sleep 1000
		objNIC.SetDNSServerSearchOrder(arrCompleteDNS)
	end if
next

"I hope I can chill and see the change - stop the bleed inside and feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top