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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DNS Change by Script

Status
Not open for further replies.

jonbutterworth

IS-IT--Management
Sep 21, 2010
17
GB
Hey Guys,

I have the following code for changing the IP Address, which seems to do nothing on my Windows 7 Machine when I test it.

And also, I am trying to create another script that will set the DNS Address settings back to Automatic.

Basically I have a temporary issue where users need a different DNS Server in the office to get email to work but then when they go to work from home, they will need to set it back to automatic to get the internet working at home.

I have found multiple scripts that say they set the address manually, although I am unable to get it to work! But I cannot find anything that will set it back to Automatic.

Ideally I'd like to have 2 x Icons on the users desktop, one saying Office and one Saying home, the office one sets the static DNS Server Address and the home one sets the automatic DNS Server address.

The code I have for setting the static address is as follows:

On Error Resume Next
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colNetCards = objWMIService.ExecQuery _
("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled = TRUE")
For Each objNetCard in colNetCards
If objNetCard.DHCPEnabled = True Then
object.Skip(3)
Else
arrDNSServers = Array("192.168.1.1", "192.168.1.2")
objNetCard.SetDNSServerSearchOrder(arrDNSServers)
End If
Next


Using 1.1 and 1.2 as test servers for this laptop, but will change them to the correct servers when I know its working.

Any help would be greatly appreciated.

Thanks,
Jon
 
Guys I've figured something out for both scripts.

The following works for assigning a DNS Server:

strComputer = "."

Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colNetCards = objWMIService.ExecQuery _
("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled = True")

For Each objNetCard in colNetCards
arrDNSServers = Array("192.168.1.1", "192.168.1.2")
objNetCard.SetDNSServerSearchOrder(arrDNSServers)
Next


And the following works for resetting to Automatic DNS Server:

strComputer = "."

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colNetCards = objWMIService.ExecQuery _
("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled = True")

For Each objNetCard in colNetCards
arrDNSServers = Array()
objNetCard.SetDNSServerSearchOrder(arrDNSServers)
Next


However, neither of these scripts work in Windows 7...

Any ideas why and what I can change to get them to work in all operating systems?

Thanks,
 
Getting there, managed to go one step further and make the script automated.

I've used a ping command to ping the dns server, if it gets a reply then the PC is on the office network and the script will change the DNS Server, if no reply from the ping, the computer is not on the office network, so the script will set the dns to automatic.

Works well in XP but still nothing in 7, what am I doing wrong?

---

On Error Resume Next

Dim strTarget, strPingResults
strTarget = "192.168.1.1"

Set WshShell = WScript.CreateObject("WScript.Shell")
Set WshExec = WshShell.Exec("ping -n 3 -w 2000 " & strTarget)
strPingResults = LCase(WshExec.StdOut.ReadAll)
If InStr(strPingResults, "reply from") Then

strComputer = "."

Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colNetCards = objWMIService.ExecQuery _
("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled = True")

For Each objNetCard in colNetCards
arrDNSServers = Array("192.168.1.1", "192.168.1.2")
objNetCard.SetDNSServerSearchOrder(arrDNSServers)
Next

Else

strComputer = "."

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colNetCards = objWMIService.ExecQuery _
("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled = True")

For Each objNetCard in colNetCards
arrDNSServers = Array()
objNetCard.SetDNSServerSearchOrder(arrDNSServers)
Next


End If
 
I'm trying to do something similar (change NIC to DHCP and set the DNS servers. I can't get it to work on Win7, either. Haven't tried it on WinXP, yet.

Code:
strComputer = "."
strDNS1 = "8.8.8.8"
StrDNS2 = "8.8.4.4"

Set objWMIService = GetObject("winmgmts:" _
        & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colNetAdapters = objWMIService.ExecQuery _
        ("Select * from Win32_NetworkAdapterConfiguration where IPEnabled = TRUE")

For Each objNetAdapter in colNetAdapters
    Confirm = MsgBox("For adapter """ & objNetAdapter.Description & """ ,do you want to set it to use DHCP and update the DNS settings?", 4, "Confirm network change")
    If Confirm = 6 Then  '6 = yes, 7 = no
            errEnable = objNetAdapter.EnableDHCP()
            arrDNSServers = Array(strDNS1,strDNS2)
      objNetAdapter.SetDNSServerSearchOrder(arrDNSServers)
        Else
        End If         
Next

I figured out a roundabout way to make my script work on my Win7 x64 machine. I created a shortcut to "wscript.exe" and gave it the location of the script. In the properties for the shortcut, I changed the target line to something like this:
Code:
C:\Windows\SysWOW64\wscript.exe "C:\Users\username\Desktop\change NICs.vbs"
 
Looks good thanks for that, however while I think it would be a valuable tool for myself, I think there is too much interaction needed by the user to be able to work it.

Unless there is a way of automating it that I've not seen yet.

This is only a temporary fix and need to involve as little user interaction as possible.

Thanks
Jon
 
PPetit,

Thanks for the additional information, I'll have a fiddle around and see if I can get it working for me.

Thanks,
 
I included the user interaction because on some machines, more than one adapter would appear (primarily VMWare virtual adapters). I didn't want them changed so I had to do a yes/no confirmation on each adapter. Also, I would be running the script myself instead of automating it or letting the users run it.

I have some other ideas on how to make your script work without the shortcut workaround: 1. PowerShell 2. script to start your script with admin credentials. If I get the time, I'll try them out and post back if I have any success.
 
hi Jon,
maybe offtopic. but:
do only a few users in the office lan need another dns to access the mailserver, or may all use that dns.
ifso, then as a network admin, I would prefer a dhcp server.

could't you add the mailserver in your localdns?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top