{"id":765,"date":"2010-06-05T14:05:49","date_gmt":"2010-06-05T19:05:49","guid":{"rendered":"http:\/\/www.jasemccarty.com\/blog\/?p=765"},"modified":"2014-06-25T18:31:59","modified_gmt":"2014-06-26T00:31:59","slug":"powercli-commands-getset-oscustomizationnicmapping","status":"publish","type":"post","link":"https:\/\/www.jasemccarty.com\/blog\/powercli-commands-getset-oscustomizationnicmapping\/","title":{"rendered":"PowerCLI cmdlets: Get\/Set-OSCustomizationNicMapping"},"content":{"rendered":"<p>At some point in the near future, I have to create several hundred new VM&#8217;s.\u00a0 These VM&#8217;s will be using the same Customization Specification, but some will need to have Static IP addresses, and others will need to have DHCP addresses that are reserved based on Mac Address.<\/p>\n<p><strong>The Task<\/strong><br \/>\nAlright, how am I going to create 400-500 VM&#8217;s with specific names and specific IP&#8217;s (with some being DHCP)?\u00a0 I know there are some tools like the RCU from NetApp that will rapidly provision VM&#8217;s, but I&#8217;m not looking for 100% clones, as not all of these are going to be used for the same purpose (not using VDI here).<\/p>\n<p>Customization Specifications typically can only be used once per VM when attempting to use a Static IP address.\u00a0 Using DHCP, a Customization Specification can be easily reused, as it doesn&#8217;t change.\u00a0 The problem is, what if I want to give each VM a Static IP.\u00a0 That requirement makes it a little more difficult.<\/p>\n<p><strong>The Tool: PowerCLI<\/strong><br \/>\nThere are a couple new cmdlets in the 4.0 Update 1 release of the PowerCLI that will help accomplish this task.<\/p>\n<ul>\n<li><strong>Get-OSCustomizationNicMapping<\/strong><\/li>\n<li><strong>Set-OSCustomizationNicMapping<\/strong><\/li>\n<\/ul>\n<p>Using these together in a PowerCLI script, settings like IP address (if static), DHCP, gateway, DNS &amp; WINS settings, and Mac Address can easily be set.<\/p>\n<p><!--more-->The syntax for these are as follows:<\/p>\n<blockquote><p><strong>Get-OSCustomizationNicMapping<\/strong><br \/>\nSYNTAX<br \/>\nGet-OSCustomizationNicMapping [-Spec] &lt;OSCustomizationSpec[]&gt; [-Server &lt;VIServer[]&gt;] [&lt;CommonParameters&gt;]<\/p>\n<p><strong>Set-OSCustomizationNicMapping<\/strong><br \/>\nSYNTAX<br \/>\nSet-OSCustomizationNicMapping -OSCustomizationNicMapping &lt;OSCustomizationNicMapping[]&gt; [-Position &lt;Int32&gt;] [-Server &lt;VIServer[]&gt;] [-IpMode &lt;OSCustomizationIPMode&gt;] [-VCApplicationArgument &lt;String&gt;] [[-IpAddress] &lt;String&gt;] [[-SubnetMask] &lt;String&gt;] [[-DefaultGateway] &lt;String&gt;] [-AlternateGateway &lt;String&gt;] [[-Dns] &lt;String[]&gt;] [-Wins &lt;String[]&gt;] [-WhatIf] [-Confirm] [&lt;CommonParameters&gt;]<\/p>\n<p>Set-OSCustomizationNicMapping -OSCustomizationNicMapping &lt;OSCustomizationNicMapping[]&gt; [-NetworkAdapterMac &lt;String&gt;] [-Server &lt;VIServer[]&gt;] [-IpMode &lt;OSCustomizationIPMode&gt;] [-VCApplicationArgument &lt;String&gt;] [[-IpAddress] &lt;String&gt;] [[-SubnetMask] &lt;String&gt;] [[-DefaultGateway] &lt;String&gt;] [-AlternateGateway &lt;String&gt;] [[-Dns] &lt;String[]&gt;] [-Wins &lt;String[]&gt;] [-WhatIf] [-Confirm] [&lt;CommonParameters&gt;]<\/p><\/blockquote>\n<p>Get-Help Get\/Set-OSCustomizationNicMapping -full can give you more information on each of these cmdlets.<\/p>\n<p><strong>My Script<\/strong><br \/>\nSo I put together a little script to read content from a file, set a nic mapping for the selected Customization Specification, clone the VM, and set the appropriate VLAN for the VM.<\/p>\n<p>The text file proceeds with <em>the assumption that the DNS and WINS servers are the same<\/em>.\u00a0 The pdnswins and sdnswins variables are used twice each, one for the primary DNS\/WINS and once for the secondary DNS\/WINS.\u00a0 Here are the contents of my text file that contains 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>I saved the file as <strong>C:vms.csv<\/strong>.\u00a0 <span style=\"text-decoration: line-through;\">Even though I named the file with a .csv extenstion, I decided to use colons rather than commas.\u00a0 I did this because I wanted to send multiple DNS\/WINS entries to my script, but I&#8217;ll get into that a little later. <\/span>I have updated the script to use the standard comma delimiter.<span style=\"text-decoration: line-through;\"><br \/>\n<\/span><\/p>\n<p>The script to clone the VM&#8217;s and set their appropriate values<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\r\n##########################################################\r\n# cloneandsetip.ps1\r\n# Jase McCarty 6\/5\/2010\r\n# Posh Script to clone VM's and set appropriate\r\n# IP addresses in Windows Virtual Machines\r\n##########################################################\r\nConnect-VIServer vcenter.jasemccarty.com\r\n\r\n$vmlist = Import-CSV C:vms.csv\r\n\r\nforeach ($item in $vmlist) {\r\n\r\n\t# I like to map out my variables\r\n\t$basevm = $item.basevm\r\n\t$datastore = $item.datastore\r\n\t$vmhost = $item.vmhost\r\n\t$custspec = $item.custspec\r\n\t$vmname = $item.vmname\r\n\t$ipaddr = $item.ipaddress\r\n\t$subnet = $item.subnet\r\n\t$gateway = $item.gateway\r\n\t$pdns = $item.pdnswins\r\n\t$pwins = $item.pdnswins\r\n\t$sdns = $item.sdnswins\r\n\t$swins = $item.sdnswins\r\n\t$vlan = $item.vlan\r\n\r\n\t#Get the Specification and set the Nic Mapping (Apply 2 DNS\/WINS if 2 are present)\r\n\tIf ($Varable) {\r\n\t\tGet-OSCustomizationSpec $custspec | Get-OSCustomizationNicMapping | Set-OSCustomizationNicMapping -IpMode UseStaticIp -IpAddress $ipaddr -SubnetMask $subnet -DefaultGateway $gateway -Dns $pdns,$sdns -Wins $pwins,$swins\r\n\t} else {\r\n\t\tGet-OSCustomizationSpec $custspec | Get-OSCustomizationNicMapping | Set-OSCustomizationNicMapping -IpMode UseStaticIp -IpAddress $ipaddr -SubnetMask $subnet -DefaultGateway $gateway -Dns $pdns -Wins $pwins\r\n\t}\r\n\r\n\t#Clone the BaseVM with the adjusted Customization Specification\r\n\tNew-VM -Name $vmname -VM $basevm -Datastore $datastore -VMHost $vmhost | Set-VM -OSCustomizationSpec $custspec -Confirm:$false\r\n\r\n\t#Set the Network Name (I often match PortGroup names with the VLAN name)\r\n\tGet-VM -Name $vmname | Get-NetworkAdapter | Set-NetworkAdapter -NetworkName $vlan -Confirm:$false\r\n\r\n\t#Remove the NicMapping (Don't like to leave things unkept)\r\n\tGet-OSCustomizationSpec $custspec | Get-OSCustomizationNicMapping | Remove-OSCustomizationNicMapping -Confirm:$false\r\n}\r\n<\/pre>\n<p>Set-OSCustomizationNicMapping is very powerful.\u00a0 Further examination of the documentation will show that things like setting the Mac Address and Alternate Gateway are also possible.<\/p>\n<p><strong>The Gotchas<\/strong><br \/>\nThere are a couple gotchas that I found, and you might want to be aware of.<\/p>\n<ul>\n<li>The setting -PrimaryWins in the documentation does not work, use -Wins. Documentation and error messages will continue to state -PrimaryWins, but that attribute doesn&#8217;t work.<\/li>\n<\/ul>\n<ul>\n<li>Multiple settings for DNS\/WINS must read something like this 192.168.0.199,192.168.0.198.I had some difficulty passing 2 DNS\/WINS entries into a single variable, so I am basically performing a check to see if a second entry is present.<\/li>\n<\/ul>\n<p><strong>In Closing<\/strong><br \/>\nThanks VMware for the very useful cmdlets to help make our jobs easier.<\/p>\n<p>For more info on each of these commands, look here for the VMware documentation:<\/p>\n<ul>\n<li><strong><a title=\"Get-OSCustomizationNicMapping\" href=\"http:\/\/www.vmware.com\/support\/developer\/windowstoolkit\/wintk40u1\/html\/Get-OSCustomizationNicMapping.html?src=vmw_so_vex_jmcca_429\" target=\"_blank\">Get-OSCustomizationNicMapping<\/a><\/strong><\/li>\n<li><strong><a title=\"Set-OSCustomizationNicMapping\" href=\"http:\/\/www.vmware.com\/support\/developer\/windowstoolkit\/wintk40u1\/html\/Set-OSCustomizationNicMapping.html?src=vmw_so_vex_jmcca_429\" target=\"_blank\">Set-OSCustomizationNicMapping<\/a><\/strong><\/li>\n<li><strong><a title=\"Remove-OSCustomizationNicMapping\" href=\"http:\/\/www.vmware.com\/support\/developer\/windowstoolkit\/wintk40u1\/html\/Remove-OSCustomizationNicMapping.html?src=vmw_so_vex_jmcca_429\" target=\"_blank\">Remove-OSCustomizationNicMapping<\/a><br \/>\n<\/strong><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>At some point in the near future, I have to create several hundred new VM&#8217;s.\u00a0 These VM&#8217;s will be using the same Customization Specification, but &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":[65,66,105,111],"class_list":["post-765","post","type-post","status-publish","format-standard","hentry","category-virtualization","tag-powercli","tag-powershell","tag-vmware","tag-vsphere"],"_links":{"self":[{"href":"https:\/\/www.jasemccarty.com\/blog\/wp-json\/wp\/v2\/posts\/765","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=765"}],"version-history":[{"count":3,"href":"https:\/\/www.jasemccarty.com\/blog\/wp-json\/wp\/v2\/posts\/765\/revisions"}],"predecessor-version":[{"id":3113,"href":"https:\/\/www.jasemccarty.com\/blog\/wp-json\/wp\/v2\/posts\/765\/revisions\/3113"}],"wp:attachment":[{"href":"https:\/\/www.jasemccarty.com\/blog\/wp-json\/wp\/v2\/media?parent=765"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jasemccarty.com\/blog\/wp-json\/wp\/v2\/categories?post=765"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jasemccarty.com\/blog\/wp-json\/wp\/v2\/tags?post=765"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}