How Can I Configure a Computer to Use a Dynamically-assigned DNS Server?

ScriptingGuy1

Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I configure a computer to use a dynamically-assigned DNS Server?

— JB

SpacerHey, Scripting Guy! AnswerScript Center

Hey, JB. Ah, this question brings back memories. A couple years ago the Scripting Guys were faced with this same problem, and discovered the answer entirely by accident; in fact, if Greg was able to type a line of code without making a mistake the truth might never have been known. Thanks to Greg’s bumbling, however, we have an answer for you.

If you aren’t sure what we’re talking about take a look at the TCP/IP properties for a network connection. As you can see, this particular network adapter has been configured to automatically obtain both an IP address and a DNS server address:

TCP/IP Properties


What we’re interested in is the second part: automatically obtaining a DNS server address. Why aren’t we interested in obtaining an IP address automatically? Because configuring a network adapter to get its IP address from a DHCP server is easy; in fact, here’s a script that does just that:

strComputer = “.”

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

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

For Each objNetAdapter In colNetAdapters errEnable = objNetAdapter.EnableDHCP() Next

It’s equally easy to assign specific DNS servers to a network adapter. Here’s a script that assigns two DNS servers – 192.168.1.100 and 192.168.1.200 – to all the IP-enabled network adapters on a computer:

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.100”, “192.168.1.200”) objNetCard.SetDNSServerSearchOrder(arrDNSServers) Next

But once assigned how do you unassign DNS servers; that is, how do you configure a network adapter to automatically obtain DNS server addresses? Setting SetDNSServerSearchOrder to Null doesn’t work; setting it to an empty string doesn’t work; setting it to an array of empty strings doesn’t work. Are DNS servers like stray cats, something you can never get rid of?

Fortunately, no. As we noted earlier, when trying to solve this problem Greg mistyped a line of code: he somehow failed to specify values for the array (DNS servers must be assigned as an array, even if you’re assigning only one such server). Lo and behold, that did the trick. Here’s the fruit of his, uh, efforts:

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

We begin by connecting to the WMI service, then retrieving a collection of all the instances of the Win32_NetworkAdapterConfiguration class where the IPEnabled property is true. That allows us to zero in on the “real” network adapters, and weed out virtual adapters, VPNs, and other non-IP-enabled connections.

After we obtain our list of IP-enabled network adapters, all the action takes place within the For Each loop that loops through that collection. We begin by assigning a completely empty array (no elements whatsoever) to a variable named arrDNSServers; that’s what happens here:

arrDNSServers = Array()

We then pass that empty array as the sole parameter to the SetDNSServerSearchOrder method:

objNetCard.SetDNSServerSearchOrder(arrDNSServers)

That’s all you need to do; from that point on all your IP-enabled network adapters will automatically obtain the address of their DNS server.

Like Greg always says, genius is overrated. Of course, when he says this he has a habit of mispronouncing the word “genius,” but, hey….

0 comments

Discussion is closed.

Feedback usabilla icon