{"id":792,"date":"2010-06-08T09:52:59","date_gmt":"2010-06-08T14:52:59","guid":{"rendered":"http:\/\/www.jasemccarty.com\/blog\/?p=792"},"modified":"2014-06-25T18:30:33","modified_gmt":"2014-06-26T00:30:33","slug":"powercli-cmdlets-getset-vmguestnetworkinterface","status":"publish","type":"post","link":"https:\/\/www.jasemccarty.com\/blog\/powercli-cmdlets-getset-vmguestnetworkinterface\/","title":{"rendered":"PowerCLI cmdlets: Get\/Set-VMGuestNetworkInterface"},"content":{"rendered":"<p>After my last post on <strong><a title=\"PowerCLI cmdlet: Get\/Set-OSCustomizationNicMapping\" href=\"\/blog\/?p=765\" target=\"_self\">Get\/Set-OSCustomizationNicMapping<\/a><\/strong>, I started looking further at what options there were to configure network settings for guests that were &#8220;already&#8221; created.<\/p>\n<p>It is one thing to be able to set the network adapter properties during the cloning process and something entirely different to do it after the fact.\u00a0 Maybe a mistake was made during the cloning process.\u00a0 More likely a network migration could be planned, and a completely different way of doing things would be needed.<\/p>\n<p><strong>Using Invoke-VMScript<\/strong><br \/>\nIn January, I wrote a post on how to <strong><a title=\"Update VMware Windows Guest DNS and WINS through PowerCLI\" href=\"\/blog\/?p=477\" target=\"_self\">Update VMware Windows Guest DNS and WINS through PowerCLI<\/a><\/strong> that uses the Invoke-VMScript cmdlet to run a batch file in Windows to set the DNS and WINS information.\u00a0 For those versed with netsh, it is powerful, <em>but there are requirements<\/em>&#8230;<\/p>\n<p>One pretty big requirement is that the\u00a0 name of the network adapter must be known.\u00a0 On older VM&#8217;s, or possibly P2V&#8217;ed VM&#8217;s the network adapter name could be something like &#8220;Local Network Connection 2&#8221;, &#8220;Local Network Connection 3&#8221;, etc.\u00a0 Using the above mentioned script is going to fail if the connection name is not known.\u00a0 The only way around that, would be to put some logic in to determine the name to use when calling the script.\u00a0 Additionally using netsh with DNS and WINS requires the <em>addition<\/em> of new values, and <em>deletion<\/em> of old values.<\/p>\n<p>Another issue with the Invoke-VMScript method, is that it requires different scripts for Windows and Linux guests.<\/p>\n<p><strong><!--more-->A Different Way<\/strong><br \/>\nSo I started to peruse the online docs for the current vSphere 4.0 Update 1 and found the Get\/Set-VMGuestNetworkInterface cmdlets.\u00a0 Just like my last post, I couldn&#8217;t really find any example scripts of anyone using it.<\/p>\n<p>Here is the syntax for these cmdlets:<\/p>\n<blockquote><p><strong>Get-VMGuestNetworkInterface<\/strong><br \/>\nSyntax<br \/>\nGet-VMGuestNetworkInterface [[-VM] &lt;VirtualMachine[]&gt;] [-VMGuest &lt;VMGuest[]&gt;] [-Server &lt;VIServer[]&gt;] [-ToolsWaitSecs &lt;Int32&gt;] [-GuestPassword &lt;SecureString&gt;] [-GuestUser &lt;String&gt;] [-GuestCredential &lt;PSCredential&gt;] [-HostPassword &lt;SecureString&gt;] [-HostUser &lt;String&gt;] [-HostCredential &lt;PSCredential&gt;] [&lt;CommonParameters&gt;]<\/p>\n<p><strong>Set-VMGuestNetworkInterface<\/strong><br \/>\nSyntax<br \/>\nSet-VMGuestNetworkInterface -VmGuestNetworkInterface &lt;VMGuestNetworkInterface[]&gt; [-WinsPolicy &lt;DhcpPolicy&gt;] [-Wins &lt;String[]&gt;] [-DnsPolicy &lt;DhcpPolicy&gt;] [-Dns &lt;String[]&gt;] [-IPPolicy &lt;DhcpPolicy&gt;] [[-Gateway] &lt;Object&gt;] [[-Netmask] &lt;String&gt;] [[-Ip] &lt;IPAddress&gt;] [-ToolsWaitSecs &lt;Int32&gt;] [-GuestPassword &lt;SecureString&gt;] [-GuestUser &lt;String&gt;] [-GuestCredential &lt;PSCredential&gt;] [-HostPassword &lt;SecureString&gt;] [-HostUser &lt;String&gt;] [-HostCredential &lt;PSCredential&gt;] [-WhatIf] [-Confirm] [&lt;CommonParameters&gt;]<\/p><\/blockquote>\n<p>One thing that struck me as odd, was that both of these commands have the following statements as part of their documentation:<\/p>\n<blockquote><p>&#8230; Consider that this functionality is experimental. &#8230;<\/p><\/blockquote>\n<p>and<\/p>\n<blockquote><p>&#8230;<strong>Notes<\/strong><br \/>\nSupported on ESX 3.5 and newer. &#8230;<\/p><\/blockquote>\n<p>So which is it? Maybe <strong><a title=\"Carter Shanklin\" href=\"http:\/\/twitter.com\/cshanklin\" target=\"_blank\">@cshanklin<\/a><\/strong> can address this at some point.<\/p>\n<p><strong>How Does It Really Work?<\/strong><br \/>\nAre the Get\/Set-VMGuestNetworkInterface cmdlets really that different from Invoke-VMScript? Not really, except for the fact that VMware has taken some of the guesswork out of the process.\u00a0 These cmdlets actually call scripts in the Scripts folder located under the installation path of the vSphere PowerCLI.\u00a0 There are separate scripts for Windows OSes and Linux OSes (currently only supported on RHEL 5).<\/p>\n<p>The netsh scripting limitation I mentioned earlier (network connection name) is actually taken care of by these scripts.\u00a0 Additionally these scripts can be modified\/added to if desired.<\/p>\n<p>There are a couple things to be aware of.<\/p>\n<ul>\n<li>Set-VMGuestNetworkInterface does not work in my environment if the vNic is disconnected.<\/li>\n<\/ul>\n<ul>\n<li>I could not use the vSphere PowerCLI in x64 mode to run the script.\u00a0 The PowerCLI stated that I needed to use the 32bit PowerCLI instead.<\/li>\n<\/ul>\n<ul>\n<li>WINS settings are not available in Linux guests.<\/li>\n<\/ul>\n<p><strong>How To Update <\/strong><br \/>\nNow how do I go and update IP addresses on several machines (possibly those that I created in my <strong><a title=\"PowerCLI cmdlet: Get\/Set-OSCustomizationNicMapping\" href=\"\/blog\/?p=765\" target=\"_self\">last post<\/a><\/strong>)?<\/p>\n<p>Here are the contents of my text file (<strong>C:vms.csv<\/strong>) that contained my VM names and other settings.<\/p>\n<blockquote><p>basevm,datastore,vmhost,custspec,vmname,ipaddress,subnet,gateway,pdnswins,sdnswins,vlan<br \/>\nBASEVM,DS1,ESXi1,W2K3,VM01,192.168.0.80,255.255.255.0,192.168.0.1,192.168.0.199,192.168.0.198,Primary<br \/>\nBASEVM,DS1,ESXi1,W2K3,VM02,192.168.0.81,255.255.255.0,192.168.0.1,192.168.0.199,192.168.0.198,Primary<br \/>\nBASEVM,DS1,ESXi1,W2K3,VM03,192.168.0.82,255.255.255.0,192.168.0.1,192.168.0.199,192.168.0.198,Primary<br \/>\nBASEVM,DS1,ESXi1,W2K3,VM04,192.168.255.83,255.255.255.0,192.168.0.1,192.168.0.199,192.168.0.198,Secondary<br \/>\nBASEVM,DS1,ESXi1,W2K3,VM05,192.168.255.84,255.255.255.0,192.168.0.1,192.168.0.199,192.168.0.198,Secondary<\/p><\/blockquote>\n<p>So I&#8217;ll modify it because I didn&#8217;t mean to use 192.168.0.x, I meant to use 192.168.1.x. And I&#8217;ll pull out the things I don&#8217;t need, like basevm, datastore, vmhost, &amp; custspec.<\/p>\n<blockquote><p>vmname,ipaddress,subnet,gateway,pdnswins,sdnswins,vlan<br \/>\nVM01,192.168.1.80,255.255.255.0,192.168.1.1,192.168.1.199,192.168.1.198,Primary<br \/>\nVM02,192.168.1.81,255.255.255.0,192.168.1.1,192.168.1.199,192.168.1.198,Primary<br \/>\nVM03,192.168.1.82,255.255.255.0,192.168.1.1,192.168.1.199,192.168.1.198,Primary<br \/>\nVM04,192.168.255.83,255.255.255.0,192.168.1.1,192.168.1.199,192.168.1.198,Secondary<br \/>\nVM05,192.168.255.84,255.255.255.0,192.168.1.1,192.168.1.199,192.168.1.198,Secondary<\/p><\/blockquote>\n<p>Now I&#8217;ll save it as <strong>C:vms-newip.csv<\/strong>.<\/p>\n<p>Also, unlike the Get\/Set-OSCustomizationNicMapping cmdlet, I&#8217;ll have to connect specifically to the host and the guest with appropriate credentials.\u00a0 I have to do this, because credentials are required in the guest to make the change.<\/p>\n<p><strong>The Script<\/strong><br \/>\nI&#8217;ll use the following script to update the VM&#8217;s.<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\r\n##########################################################\r\n# updateip.ps1\r\n# Jase McCarty 6\/6\/2010\r\n# Posh Script to update IP\r\n# addresses in Virtual Machines\r\n##########################################################\r\n\r\nConnect-VIServer vcenter.jasemccarty.com\r\n\r\n$HostCred = $Host.UI.PromptForCredential(&amp;quot;Please enter credentials&amp;quot;, &amp;quot;Enter ESX host credentials&amp;quot;, &amp;quot;&amp;quot;, &amp;quot;&amp;quot;)\r\n$GuestCred = $Host.UI.PromptForCredential(&amp;quot;Please enter credentials&amp;quot;, &amp;quot;Enter Guest credentials&amp;quot;, &amp;quot;&amp;quot;, &amp;quot;&amp;quot;)\r\n\r\n$vmlist = Import-CSV C:vms.csv\r\n\r\nforeach ($item in $vmlist) {\r\n\r\n  # I like to map out my variables\r\n  $vmname = $item.vmname\r\n  $ipaddr = $item.ipaddress\r\n  $subnet = $item.subnet\r\n  $gateway = $item.gateway\r\n  $pdnswins = $item.pdnswins\r\n  $sdnswins = $item.sdnswins\r\n\r\n  #Get the current interface info\r\n  $GuestInterface = Get-VMGuestNetworkInterface -VM $vmname -HostCredential $HostCred -GuestCredential $GuestCred\r\n\r\n  #If the IP in the VM matches, then I don't need to update\r\n  If ($ipaddr -ne $($GuestInterface.ip)) {\r\n      Set-VMGuestNetworkInterface\u00a0 -VMGuestNetworkInterface $GuestInterface -HostCredential $HostCred -GuestCredential $GuestCred -IP $ipaddr -Netmask $subnet -Gateway $gateway -DNS $pdnswins,$sdnswins -WINS $pdnswins,$sdnswins\r\n  }\r\n}\r\n<\/pre>\n<p>As can be seen, it doesn&#8217;t take a complex script to update IP information in guests.<\/p>\n<p>Keep in mind that not all of the attributes that I chose to use are required.\u00a0 If desired, only the IP, DNS, WINS, etc can be updated.\u00a0 My script just happens to update several of these items.\u00a0 Additionally, this can be used to change a guest from a static IP address to a DHCP address as well.<\/p>\n<p>Hopefully this script will help you when presented with a similar task of updating IP addresses\/DNS\/WINS settings.<\/p>\n<p><strong>For More Info<\/strong><br \/>\nFor more info on each of these commands, look here for the VMware documentation:<\/p>\n<ul>\n<ul>\n<li><strong><a title=\"Get-VMGuestNetworkInterface\" href=\"http:\/\/www.vmware.com\/support\/developer\/windowstoolkit\/wintk40u1\/html\/Get-VMGuestNetworkInterface.html?src=vmw_so_vex_jmcca_429\" target=\"_blank\">Get-VMGuestNetworkInterface<\/a><\/strong><\/li>\n<li><strong><a title=\"Set-VMGuestNetworkInterface\" href=\"http:\/\/www.vmware.com\/support\/developer\/windowstoolkit\/wintk40u1\/html\/Set-VMGuestNetworkInterface.html?src=vmw_so_vex_jmcca_429\" target=\"_blank\">Set-VMGuestNetworkInterface<\/a><\/strong><\/li>\n<\/ul>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>After my last post on Get\/Set-OSCustomizationNicMapping, I started looking further at what options there were to configure network settings for guests that were &#8220;already&#8221; created. &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":[57,65,66,105,111],"class_list":["post-792","post","type-post","status-publish","format-standard","hentry","category-virtualization","tag-netsh","tag-powercli","tag-powershell","tag-vmware","tag-vsphere"],"_links":{"self":[{"href":"https:\/\/www.jasemccarty.com\/blog\/wp-json\/wp\/v2\/posts\/792","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=792"}],"version-history":[{"count":5,"href":"https:\/\/www.jasemccarty.com\/blog\/wp-json\/wp\/v2\/posts\/792\/revisions"}],"predecessor-version":[{"id":3111,"href":"https:\/\/www.jasemccarty.com\/blog\/wp-json\/wp\/v2\/posts\/792\/revisions\/3111"}],"wp:attachment":[{"href":"https:\/\/www.jasemccarty.com\/blog\/wp-json\/wp\/v2\/media?parent=792"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jasemccarty.com\/blog\/wp-json\/wp\/v2\/categories?post=792"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jasemccarty.com\/blog\/wp-json\/wp\/v2\/tags?post=792"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}