4

I need to switch Internet Protocol Version 4 (TCP/IPv4) and Internet Protocol Version 6 (TCP/IPv6) settings for DNS server configuration from "Obtain DNS server address automatically" to hard coded values (for Google's DNS servers) and back. It's kind of a hassle to do manually (I need to go to Control Panel-Network and Internet-Network Connections, open Wireless Network Connection 2, then open Properties for the corresponding version, enter the DNS server values, and repeat for the other one). Is there a utility or script that would automate the process? Thanks.

Alek Davis
  • 153
  • 1
  • 1
  • 7
  • 1
    Can't you just have the DHCP server change them as needed? – Shane Madden Apr 18 '12 at 05:31
  • Not sure what you mean, Shane. I'm trying to implement a workaround for AT&T router/DNS issue on the client side (here is background: http://goo.gl/OjcXx). – Alek Davis Apr 18 '12 at 05:46
  • Do you really need to do this for IPv6? If so, why? – joeqwerty Apr 18 '12 at 10:32
  • I'm not sure if I really need to. It was in the instructions for the workaround (for the router issue) I saw somewhere, but it may work with just changing IPv4. Would be nice to be able to take care of IPv6 just in case. – Alek Davis Apr 18 '12 at 14:26

3 Answers3

8

In powershell:

$wmi = Get-WmiObject win32_networkadapterconfiguration -filter "ipenabled = 'true'" 
$DNSServers = "8.8.8.8","8.8.4.4"  
$wmi.SetDNSServerSearchOrder($DNSServers)

To revert:

$wmi.SetDNSServerSearchOrder()

(no parameters=null, this will put it back in automatic mode)

References:

http://fatbeards.blogspot.com/2008/06/change-dnswins-ip-on-multiple-servers.html

http://blogs.technet.com/b/heyscriptingguy/archive/2012/02/28/use-powershell-to-configure-static-ip-and-dns-settings.aspx

Lucas Kauffman
  • 16,880
  • 9
  • 58
  • 93
  • Thanks Lucas. I assume this is for IPv4. How do you get the WMI object for IPv6? Also, how do you switch it back to "Obtain DNS server address automatically"? – Alek Davis Apr 18 '12 at 05:42
  • I can't seem to find how to do it in powershell with IPv6. To obtain a DNS address automatically you would need to do a new DHCP request, there isn't a way (I'm aware off) to just got the DNS server from a DHCP server. – Lucas Kauffman Apr 18 '12 at 06:05
  • That's okay. What about going back it back to "Obtain DNS server address automatically"? – Alek Davis Apr 18 '12 at 14:27
  • I'll add it to the answer – Lucas Kauffman Apr 18 '12 at 18:08
  • Great, thanks. I'll mark is as the accepted answer, but if anyone has an idea hot to handle IPv6 or can offer other alternatives (e.g. do the same without PowerShell, please post it). – Alek Davis Apr 18 '12 at 21:39
  • Okay, just tried and it did not seem to work. After second statement, I just get the following output: __GENUS : 2 __CLASS : __PARAMETERS __SUPERCLASS : __DYNASTY : __PARAMETERS __RELPATH : __PROPERTY_COUNT : 1 __DERIVATION : {} __SERVER : __NAMESPACE : __PATH : ReturnValue : 91 – Alek Davis Apr 19 '12 at 04:38
  • Okay try this, I can't test it atm – Lucas Kauffman Apr 19 '12 at 06:21
2

From elevated command prompt (or from .bat file):

netsh interface ipv4 set dnsserver "Wireless Network Connection 2" source=static addr=8.8.8.8
netsh interface ipv4 add dnsserver "Wireless Network Connection 2" addr=8.8.4.4 index=2

or

netsh interface ipv4 set dnsserver "Wireless Network Connection 2" source=dhcp

You can just replace ipv4 with ipv6 (and proper address of course).

Dusan Bajic
  • 2,056
  • 1
  • 18
  • 21
  • Great, thanks. Only for some reason the second command (the one that sets DNS server to 8.8.4.4) causes error: "'source' is not a valid argument for this command. The syntax supplied for this command is not valid. Check help for the correct syntax." – Alek Davis Apr 20 '12 at 04:24
  • ah, yes, source=static is not needed when adding the second nameserver. I have edited the answer – Dusan Bajic Apr 20 '12 at 07:41
1

Some manufacturers (IBM/Lenovo, Sony to mention just a few) used to supply users with their own (branded) utilities to solve that. It mostly used on notebook PCs: when you notebook connected to 'known' network, the utility automatically set up whatever you need to live in that network (proxy, DNS, maybe secondary IPs on the same interfaces). If you're using that kind of system, try to use that option.

On the other hand, why don't you set that up on DCHP servers in that networks? Static (by MAC) settings are something not hard to implement, anyway.

Alexander
  • 774
  • 2
  • 11
  • 20
  • Because I do not own the corporate network. – Alek Davis May 04 '12 at 16:36
  • 1
    No need to own the network to have some talk with 'network' guys so they create some settings for you (if it won't be against your corporate rules) :) Ok, here is the utilities for you to consider: [Free IP Switcher](http://www.eusing.com/ipswitch/free_ip_switcher.htm) or [Net Profiles](http://code.google.com/p/netprofiles/). – Alexander May 04 '12 at 20:48
  • Having some talk with 'network' guys? Ha-ha-ha... Very funny. Thanks for the links to the apps. They look like what I may have needed in the first place (would be better than a script option). Appreciate it. – Alek Davis May 05 '12 at 00:01
  • Okay, tried both and uninstalled. Net Profiles does not seem to do anything. Launched is normally and as an admin: nothing happens. IP Switcher looks a lot more messy than the script-based solution. Btw, when will people learn to build installers that automatically run as administrators. It's 2012 for crying out loud. – Alek Davis May 05 '12 at 05:32
  • Sorry to hear that, once one of it (Net Profiles) has helped me very nice, but that was on XP (and it was long ago and maybe different SP version). Look at [NetSetMan](http://www.netsetman.com/), looks promising (and claims to be freeware for non-commercial purposes)... – Alexander May 05 '12 at 05:55
  • Just checked again - Net Profiles simple fails to start at all at my Windows 7 Prof x64 SP1. Sorry for recommending that – Alexander May 05 '12 at 06:00