{"id":477,"date":"2010-01-22T15:05:19","date_gmt":"2010-01-22T20:05:19","guid":{"rendered":"http:\/\/www.jasemccarty.com\/blog\/?p=477"},"modified":"2010-01-22T15:05:19","modified_gmt":"2010-01-22T20:05:19","slug":"update-vmware-windows-guest-dns-and-wins-through-powercli","status":"publish","type":"post","link":"https:\/\/www.jasemccarty.com\/blog\/update-vmware-windows-guest-dns-and-wins-through-powercli\/","title":{"rendered":"Update VMware Windows Guest DNS and WINS through PowerCLI"},"content":{"rendered":"<p>I was really taken with Arnim van Lieshout&#8217;s <strong><a title=\"PowerCLI: Get WMI info from isolated guests\" href=\"http:\/\/www.van-lieshout.com\/2010\/01\/powercli-get-wmi-info-from-isolated-guests\/\" target=\"_blank\">post<\/a><\/strong> on running getting WMI from inside a guest through the PowerCLI, so I decided to play around a little bit with this.<\/p>\n<p>The ability of the PowerCLI to use the Invoke-VMScript command is really a great addition to the set of tools the PowerCLI provides.<\/p>\n<p>I typically only have Windows guests, minus a couple VMware appliances, so I decide to focus on how to leverage this functionality within Windows.\u00a0 There are many articles out there on how to use netsh to update DNS and WINS settings.<\/p>\n<p><strong>A typical set of commands to update DNS and WINS look like this:<\/strong><\/p>\n<blockquote><p>netsh interface ip set dns &#8220;Local Area Connection&#8221; static x.x.x.x<br \/>\nnetsh interface ip add dns &#8220;Local Area Connection&#8221; x.x.x.y<br \/>\nnetsh interface ip delete wins &#8220;Local Area Connection&#8221; x.x.x.a<br \/>\nnetsh interface ip delete wins &#8220;Local Area Connection&#8221; x.x.x.b<br \/>\nnetsh interface ip add wins &#8220;Local Area Connection&#8221; x.x.x.x<br \/>\nnetsh interface ip add wins &#8220;Local Area Connection&#8221; x.x.x.y index=2<\/p>\n<p>*** Where x.x.x.x is the new primary DNS\/WINS, x.x.x.y is the new secondary DNS, x.x.x.a is the old primary WINS, and the old secondary WINS.<\/p><\/blockquote>\n<p>Not a difficult command, provided the guest&#8217;s network adapter is named the default of &#8220;Local Area Connection&#8221;.<\/p>\n<p>Netsh can easily set a primary DNS and add a secondary DNS, which overwrites the current settings.\u00a0 WINS on the other hand, can only have WINS addresses added or deleted.\u00a0 So I chose to set the DNS, delete old WINS entries, and add new WINS entries.<\/p>\n<p><strong>My first attempt at creating this script looks like this:<\/strong><\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\n$VIServer = Read-Host &quot;Enter vCenter Server Name or IP: &quot;\nConnect-VIServer $VIServer\n\n$HostCred = $Host.UI.PromptForCredential(&quot;Please enter credentials&quot;, &quot;Enter ESX host credentials&quot;, &quot;&quot;, &quot;&quot;)\n$GuestCred = $Host.UI.PromptForCredential(&quot;Please enter credentials&quot;, &quot;Enter Guest credentials&quot;, &quot;&quot;, &quot;&quot;)\n$PrimaryDNS = Read-Host &quot;Primary DNS: &quot;\n$SecondaryDNS = Read-Host &quot;Secondary DNS: &quot;\n$PrimaryOldWINS = Read-Host &quot;Old Primary WINS: &quot;\n$SecondaryOldWINS = Read-Host &quot;Old Secondary WINS: &quot;\n$PrimaryWINS = Read-Host &quot;Primary WINS: &quot;\n$SecondaryWINS = Read-Host &quot;Secondary WINS: &quot;\n\nget-vm |\u00a0 %{ $_.Name; $_ | Invoke-VMScript -HostCredential $HostCred -GuestCredential $GuestCred -ScriptType &quot;bat&quot; -ScriptText &quot;netsh interface ip set dns &quot;&quot;Local Area Connection&quot;&quot; static $PrimaryDNS&quot; }\nget-vm |\u00a0 %{ $_.Name; $_ | Invoke-VMScript -HostCredential $HostCred -GuestCredential $GuestCred -ScriptType &quot;bat&quot; -ScriptText &quot;netsh interface ip add dns &quot;&quot;Local Area Connection&quot;&quot; $SecondaryDNS&quot; }\nget-vm |\u00a0 %{ $_.Name; $_ | Invoke-VMScript -HostCredential $HostCred -GuestCredential $GuestCred -ScriptType &quot;bat&quot; -ScriptText &quot;netsh interface ip delete wins &quot;&quot;Local Area Connection&quot;&quot; $PrimaryOldWINS&quot; }\nget-vm |\u00a0 %{ $_.Name; $_ | Invoke-VMScript -HostCredential $HostCred -GuestCredential $GuestCred -ScriptType &quot;bat&quot; -ScriptText &quot;netsh interface ip delete wins &quot;&quot;Local Area Connection&quot;&quot; $SecondaryOldWINS&quot; }\nget-vm |\u00a0 %{ $_.Name; $_ | Invoke-VMScript -HostCredential $HostCred -GuestCredential $GuestCred -ScriptType &quot;bat&quot; -ScriptText &quot;netsh interface ip add wins &quot;&quot;Local Area Connection&quot;&quot; $PrimaryWINS&quot; }\nget-vm |\u00a0 %{ $_.Name; $_ | Invoke-VMScript -HostCredential $HostCred -GuestCredential $GuestCred -ScriptType &quot;bat&quot; -ScriptText &quot;netsh interface ip add wins &quot;&quot;Local Area Connection&quot;&quot; $SecondaryWINS index=2&quot; }\n<\/pre>\n<p>It works, but it is a little ugly because it performs an action against a guest, then proceeds to the next, and so on.\u00a0 When the first command is done, it loops through all the VM&#8217;s and performs the second command.\u00a0 This isn&#8217;t really an efficient way of doing this.<\/p>\n<p>So I pulled &#8220;&amp;&amp;&#8221; out of my old bag of DOS tricks to see if I could run multiple netsh commands simultaneous.<\/p>\n<p><strong>From inside a VM, I ran a combined netsh command, which looked something like this:<\/strong><\/p>\n<blockquote><p>netsh interface ip set dns &#8220;Local Area Connection&#8221; static x.x.x.x &amp;&amp; netsh interface ip add dns &#8220;Local Area Connection&#8221; x.x.x.y &amp;&amp; netsh interface ip delete wins &#8220;Local Area Connection&#8221; x.x.x.a &amp;&amp; netsh interface ip delete wins &#8220;Local Area Connection&#8221; x.x.x.b &amp;&amp; netsh interface ip add wins &#8220;Local Area Connection&#8221; x.x.x.x &amp;&amp; netsh interface ip add wins &#8220;Local Area Connection&#8221; x.x.x.y index=2<\/p><\/blockquote>\n<p>Success!<\/p>\n<p><strong>Now to incorporate this into my PowerCLI script.\u00a0 Here&#8217;s what I came up with:<\/strong><\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\n$VIServer = Read-Host &quot;Enter vCenter Server Name or IP: &quot;\nConnect-VIServer $VIServer\n\n$HostCred = $Host.UI.PromptForCredential(&quot;Please enter credentials&quot;, &quot;Enter ESX host credentials&quot;, &quot;&quot;, &quot;&quot;)\n$GuestCred = $Host.UI.PromptForCredential(&quot;Please enter credentials&quot;, &quot;Enter Guest credentials&quot;, &quot;&quot;, &quot;&quot;)\n$PrimaryDNS = Read-Host &quot;Primary DNS: &quot;\n$SecondaryDNS = Read-Host &quot;Secondary DNS: &quot;\n$PrimaryOldWINS = Read-Host &quot;Old Primary WINS: &quot;\n$SecondaryOldWINS = Read-Host &quot;Old Secondary WINS: &quot;\n$PrimaryWINS = Read-Host &quot;Primary WINS: &quot;\n$SecondaryWINS = Read-Host &quot;Secondary WINS: &quot;\n\nget-vm |\u00a0 %{ $_.Name; $_ | Invoke-VMScript -HostCredential $HostCred -GuestCredential $GuestCred -ScriptType &quot;bat&quot; -ScriptText &quot;netsh interface ip set dns &quot;&quot;Local Area Connection&quot;&quot; static $PrimaryDNS &amp;&amp; netsh interface ip add dns &quot;&quot;Local Area Connection&quot;&quot; $SecondaryDNS &amp;&amp; netsh interface ip delete wins &quot;&quot;Local Area Connection&quot;&quot; $PrimaryOldWINS &amp;&amp; netsh interface ip delete wins &quot;&quot;Local Area Connection&quot;&quot; $SecondaryOldWins &amp;&amp; netsh interface ip add wins &quot;&quot;Local Area Connection&quot;&quot; $PrimaryWINS &amp;&amp; netsh interface ip add wins &quot;&quot;Local Area Connection&quot;&quot; $SecondaryWINS index=2&quot; }\n<\/pre>\n<p>A much more efficient script, as it only initiates the netsh commands to the guest once per guest.\u00a0 I can really use this on a project I will be implementing at some point in the future.<\/p>\n<p><strong>***Some things to note:<\/strong><\/p>\n<ul>\n<li>If there are any errors with WINS servers existing inside the guest (<em>during the WINS removal piece<\/em>), the DOS commands will bomb out.\u00a0 This is just the nature of the beast when using DOS commands in this fashion.<\/li>\n<li>This script assumes that the credentials for the Hosts is the same for all hosts.<\/li>\n<li>This script assumes that the guest credentials are the same for all guests.<\/li>\n<li>The guest has to be powered on with the VMware Tools running.<\/li>\n<\/ul>\n<p>I have tested this on <strong>ESX 4.0<\/strong> with <strong>Windows 2003 guests<\/strong> and have seen <strong>success<\/strong>.\u00a0 Now off to test it against ESX 3.5 hosts and ESX 4.0 U1 hosts. I will also confirm that this <span style=\"text-decoration: underline;\">does not work in Windows 2000 guests<\/span>.\u00a0 I have not tested this on Windows 2008\/R2 guests.<\/p>\n<p>Maybe I&#8217;ll put in some error checking into it, and possibly troubleshoot the Windows 2000 issue.<\/p>\n<p>Hope it helps anyone out looking for a solution to this task.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I was really taken with Arnim van Lieshout&#8217;s post on running getting WMI from inside a guest through the PowerCLI, so I decided to play &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[32,57,65,115],"class_list":["post-477","post","type-post","status-publish","format-standard","hentry","category-virtualization","tag-dns","tag-netsh","tag-powercli","tag-wins"],"_links":{"self":[{"href":"https:\/\/www.jasemccarty.com\/blog\/wp-json\/wp\/v2\/posts\/477","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jasemccarty.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jasemccarty.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jasemccarty.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jasemccarty.com\/blog\/wp-json\/wp\/v2\/comments?post=477"}],"version-history":[{"count":0,"href":"https:\/\/www.jasemccarty.com\/blog\/wp-json\/wp\/v2\/posts\/477\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.jasemccarty.com\/blog\/wp-json\/wp\/v2\/media?parent=477"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jasemccarty.com\/blog\/wp-json\/wp\/v2\/categories?post=477"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jasemccarty.com\/blog\/wp-json\/wp\/v2\/tags?post=477"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}