{"id":69953,"date":"2005-04-25T16:24:00","date_gmt":"2005-04-25T16:24:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/04\/25\/how-can-i-configure-a-computer-to-use-a-dynamically-assigned-dns-server\/"},"modified":"2005-04-25T16:24:00","modified_gmt":"2005-04-25T16:24:00","slug":"how-can-i-configure-a-computer-to-use-a-dynamically-assigned-dns-server","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-configure-a-computer-to-use-a-dynamically-assigned-dns-server\/","title":{"rendered":"How Can I Configure a Computer to Use a Dynamically-assigned DNS Server?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"> \n<P>Hey, Scripting Guy! How can I configure a computer to use a dynamically-assigned DNS Server?<BR><BR>&#8212; JB<\/P><IMG border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" border=\"0\" alt=\"Script Center\" align=\"right\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" height=\"288\"><\/A> \n<P>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\u2019s bumbling, however, we have an answer for you.<\/P>\n<P>If you aren\u2019t sure what we\u2019re 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:<\/P><IMG border=\"0\" alt=\"TCP\/IP Properties\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/tcpip.jpg\" width=\"335\" height=\"377\"> \n<P><BR>What we\u2019re interested in is the second part: automatically obtaining a DNS server address. Why aren\u2019t 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\u2019s a script that does just that:<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;<\/p>\n<p>Set objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)<\/p>\n<p>Set colNetAdapters = objWMIService.ExecQuery _\n    (&#8220;Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE&#8221;)<\/p>\n<p>For Each objNetAdapter In colNetAdapters\n    errEnable = objNetAdapter.EnableDHCP()\nNext\n<\/PRE>\n<P>It\u2019s equally easy to assign specific DNS servers to a network adapter. Here\u2019s a script that assigns two DNS servers &#8211; 192.168.1.100 and 192.168.1.200 &#8211; to all the IP-enabled network adapters on a computer:<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;<\/p>\n<p>Set objWMIService = GetObject(&#8220;winmgmts:&#8221; _\n    &amp; &#8220;{impersonationLevel=impersonate}!\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)<\/p>\n<p>Set colNetCards = objWMIService.ExecQuery _\n    (&#8220;Select * From Win32_NetworkAdapterConfiguration Where IPEnabled = True&#8221;)<\/p>\n<p>For Each objNetCard in colNetCards\n    arrDNSServers = Array(&#8220;192.168.1.100&#8221;, &#8220;192.168.1.200&#8221;)\n    objNetCard.SetDNSServerSearchOrder(arrDNSServers)\nNext\n<\/PRE>\n<P>But once assigned how do you <I>unassign<\/I> DNS servers; that is, how do you configure a network adapter to automatically obtain DNS server addresses? Setting <B>SetDNSServerSearchOrder<\/B> to Null doesn\u2019t work; setting it to an empty string doesn\u2019t work; setting it to an array of empty strings doesn\u2019t work. Are DNS servers like stray cats, something you can <I>never<\/I> get rid of?<\/P>\n<P>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\u2019re assigning only one such server). Lo and behold, that did the trick. Here\u2019s the fruit of his, uh, efforts:<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;<\/p>\n<p>Set objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)<\/p>\n<p>Set colNetCards = objWMIService.ExecQuery _\n    (&#8220;Select * From Win32_NetworkAdapterConfiguration Where IPEnabled = True&#8221;)<\/p>\n<p>For Each objNetCard in colNetCards\n    arrDNSServers = Array()\n    objNetCard.SetDNSServerSearchOrder(arrDNSServers)\nNext\n<\/PRE>\n<P>We begin by connecting to the WMI service, then retrieving a collection of all the instances of the <B>Win32_NetworkAdapterConfiguration <\/B>class where the <B>IPEnabled<\/B> property is true. That allows us to zero in on the \u201creal\u201d network adapters, and weed out virtual adapters, VPNs, and other non-IP-enabled connections.<\/P>\n<P>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\u2019s what happens here:<\/P><PRE class=\"codeSample\">arrDNSServers = Array()\n<\/PRE>\n<P>We then pass that empty array as the sole parameter to the SetDNSServerSearchOrder method:<\/P><PRE class=\"codeSample\">objNetCard.SetDNSServerSearchOrder(arrDNSServers)\n<\/PRE>\n<P>That\u2019s all you need to do; from that point on all your IP-enabled network adapters will automatically obtain the address of their DNS server.<\/P>\n<P>Like Greg always says, genius is overrated. Of course, when he says this he has a habit of mispronouncing the word \u201cgenius,\u201d but, hey\u2026.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I configure a computer to use a dynamically-assigned DNS Server?&#8212; JB 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 [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[36,37,3,5],"class_list":["post-69953","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-client-side-management","tag-networking","tag-scripting-guy","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I configure a computer to use a dynamically-assigned DNS Server?&#8212; JB 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 [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/69953","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=69953"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/69953\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=69953"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=69953"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=69953"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}