<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Import-VApp Archives - LucD notes</title>
	<atom:link href="https://www.lucd.info/tag/import-vapp/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.lucd.info/tag/import-vapp/</link>
	<description>My PowerShell ramblings</description>
	<lastBuildDate>Wed, 08 Apr 2020 11:08:22 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.10</generator>

<image>
	<url>https://www.lucd.info/wp-content/uploads/2018/12/cropped-120px-Tibetan_Dharmacakra-32x32.png</url>
	<title>Import-VApp Archives - LucD notes</title>
	<link>https://www.lucd.info/tag/import-vapp/</link>
	<width>32</width>
	<height>32</height>
</image> 
<atom:link rel="hub" href="https://pubsubhubbub.appspot.com"/><atom:link rel="hub" href="https://pubsubhubbub.superfeedr.com"/><atom:link rel="hub" href="https://websubhub.com/hub"/>	<item>
		<title>Upgrade an Appliance with OvfTools</title>
		<link>https://www.lucd.info/2017/01/10/upgrade-appliance-ovftools/</link>
					<comments>https://www.lucd.info/2017/01/10/upgrade-appliance-ovftools/#comments</comments>
		
		<dc:creator><![CDATA[LucD]]></dc:creator>
		<pubDate>Tue, 10 Jan 2017 18:43:44 +0000</pubDate>
				<category><![CDATA[Appliance]]></category>
		<category><![CDATA[Import-VApp]]></category>
		<category><![CDATA[Ovf]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Property]]></category>
		<category><![CDATA[OpBot]]></category>
		<category><![CDATA[Opvizor]]></category>
		<category><![CDATA[OVF]]></category>
		<guid isPermaLink="false">http://www.lucd.info/?p=5518</guid>

					<description><![CDATA[Appliances are hot! Each appliance, delivered as an OVA or OVF, can have [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>Appliances are hot!</p>
<p>Each appliance, delivered as an <a href="https://en.wikipedia.org/wiki/Open_Virtualization_Format" target="_blank" rel="noopener noreferrer">OVA or OVF</a>, can have one or more <strong>properties</strong> attached to it. These properties are mostly used to configure the appliance. In <a href="https://communities.vmware.com/community/vmtn/automationtools/powercli" target="_blank" rel="noopener noreferrer">PowerCLI</a> we have the <a href="https://www.vmware.com/support/developer/PowerCLI/PowerCLI65R1/html/Get-OvfConfiguration.html" target="_blank" rel="noopener noreferrer">Get-OvfConfiguration</a> cmdlet. It returns the <strong>user configurable properties</strong> from an OVF or OVA file in a hash table.</p>
<p>But what about upgrading an appliance? Most, if not all, of the Ovf Properties are already entered for the older version of the appliance. Can&#8217;t we use that information to upgrade the appliance? And avoid having to retype all that information?</p>
<p style="padding-left: 30px;"><a href="https://www.lucd.info/2017/01/10/upgrade-appliance-ovftools/ovftools/" rel="attachment wp-att-5519"><img fetchpriority="high" decoding="async" class="alignnone wp-image-5519 " src="https://www.lucd.info/wp-content/uploads/2017/01/OvfTools.png" width="400" height="342" srcset="https://www.lucd.info/wp-content/uploads/2017/01/OvfTools.png 505w, https://www.lucd.info/wp-content/uploads/2017/01/OvfTools-300x257.png 300w" sizes="(max-width: 400px) 100vw, 400px" /></a></p>
<p>And to take away the suspense, of course, that can be done with a bit of PowerShell!</p>
<p><span id="more-5518"></span></p>
<h2>The OvfTools</h2>
<p></p><pre class="urvanov-syntax-highlighter-plain-tag">function Get-OvfVm
{
  [cmdletbinding()]
  param(
    [Parameter(Mandatory=$true)]
    [PSObject]$VM
  )

  Process{
    if($VM -is [STring]){
      $VM = Get-VM -Name $VM
    }
    $result = New-Object PSObject -Property @{
      VmName = $vm.Name
      OvfProperties = @()
    }

    $vm.ExtensionData.Config.VAppConfig.Property | 
    where{$_.UserConfigurable} | %{
      $obj = [ordered]@{
        Name = $_.Id
        Value = $_.Value
        Type = $_.Type
        Label = $_.Label
        Description = $_.Description
      }
      $result.ovfProperties += New-Object PSOBject -Property $obj
    }
    $result
  }
}

function Update-OvfVm
{
  [cmdletbinding()]
  param(
    [Parameter(Mandatory=$true)]
    [PSObject]$VM,
    [Parameter(Mandatory=$true)]
    [String]$Path,
    [Parameter(Mandatory=$true,ValueFromPipeline=$true)]
    [PSObject]$OvfProperties,
    [PSCredential[]]$Credential,
    [Parameter(Mandatory=$true)]
    [PSOBject]$VMHost,
    [Parameter(Mandatory=$true)]
    [PSObject]$Datastore,
    [String]$Suffix = 'new'
  )

  Process{
    if($VM -is [String]){
      $VM = Get-VM -Name $VM
    }
    if($VMHost -is [String]){
      $VMHost = Get-VMHost -Name $VMHost
    }
    if($Datastore -is [String]){
      $Datastore = Get-Datastore -Name $Datastore
    }

    $OVfHash = @{}
    foreach($prop in $OvfProperties.OvfProperties){
      if($OVfHash.ContainsKey($prop.Name)){
        Write-Error -Message "Name $($prop.Name) appears twice!"
      }
      else{
        if($prop.Type -eq 'password'){
          $OVfHash.Add($prop.Name,($Credential | where{$prop.Name -eq $_.UserName} | select -ExpandProperty Password))
        }
        else{
          $OVfHash.Add($prop.Name,$prop.Value)
        }
      }
    }
    $sVApp = @{
      Source = $Path
      Name = "$($VM.Name)_$($Suffix)"
      OvfConfiguration = $OVfHash
      VMHost = $VMHost
      Datastore = $Datastore
    }
    Import-VApp @SVApp
  }
}</pre><p></p>
<h3>Annotations</h3>
<p><strong>Line 18</strong>: The vApp properties can be found under the <strong>VAppConfig</strong> property in a <a href="https://pubs.vmware.com/vsphere-60/topic/com.vmware.wssdk.apiref.doc/vim.vApp.VAppConfigInfo.html" target="_blank" rel="noopener noreferrer">VAppConfigInfo</a> object.</p>
<p><strong>Line 19</strong>: In the current version of the function we only look at the <strong>UserConfigurable</strong> properties.</p>
<p><strong>Line 43</strong>: The Update-OvfVm function can accept multiple PSCredential objects. And in this way can handle multiple Ovf Properties of type <strong>password</strong>.</p>
<p><strong>Line 52-60</strong>: Poor man&#8217;s version of <a href="https://www.vmware.com/support/developer/PowerCLI/PowerCLI65R1/html/about_obn.html" target="_blank" rel="noopener noreferrer">OBN</a>.</p>
<p><strong>Line 68-70</strong>: The lines that handle Ovf Properties of type password.</p>
<p><strong>Line 83</strong>: Use the <a href="https://www.vmware.com/support/developer/PowerCLI/PowerCLI65R1/html/Import-VApp.html" target="_blank" rel="noopener noreferrer">Import-VApp</a> cmdlet with the self-constructed OvfConfiguration hash table to import the new appliance.</p>
<h2>Sample Run with OvfTools</h2>
<p>A recent Appliance that is seeing quite a bit of updates, is the OpBot appliance from <a href="https://www.opvizor.com/" target="_blank" rel="noopener noreferrer">Opvizor</a>. See my <a href="https://www.lucd.info/2016/12/01/chatops-bot/" target="_blank" rel="noopener noreferrer">ChatOps Bot</a> post for more information and further links.</p>
<p>The OpBot appliance has quite a number of properties that need to be provided on installation.</p>
<p><a href="https://www.lucd.info/2017/01/10/upgrade-appliance-ovftools/ovftools1/" rel="attachment wp-att-5522"><img decoding="async" class="alignnone wp-image-5522 size-medium" src="https://www.lucd.info/wp-content/uploads/2017/01/ovftools1-300x87.png" width="300" height="87" srcset="https://www.lucd.info/wp-content/uploads/2017/01/ovftools1-300x87.png 300w, https://www.lucd.info/wp-content/uploads/2017/01/ovftools1-720x209.png 720w, https://www.lucd.info/wp-content/uploads/2017/01/ovftools1.png 730w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<p>Once installed you can check these Ovf Properties via the vApp Options page. In the case of the OpBot appliance, this also contains the Slack integration token.</p>
<p><a href="https://www.lucd.info/2017/01/10/upgrade-appliance-ovftools/ovftools2/" rel="attachment wp-att-5521"><img decoding="async" class="alignnone wp-image-5521 size-medium" src="https://www.lucd.info/wp-content/uploads/2017/01/ovftools2-300x263.png" width="300" height="263" srcset="https://www.lucd.info/wp-content/uploads/2017/01/ovftools2-300x263.png 300w, https://www.lucd.info/wp-content/uploads/2017/01/ovftools2.png 550w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<p>With the help of the OvfTools functions, I&#8217;m going to upgrade my OpBot v2.0 installation to the newer OpBot v2.0.1 version. And that without having to retype all the Ovf Properties.</p>
<p>The OvfTools functions assume that the version from which you want to copy the Ovf Properties is installed. The appliance doesn&#8217;t need to be running. First let&#8217;s have a look at what the Get-OvfVm function returns.</p><pre class="urvanov-syntax-highlighter-plain-tag">Get-OvfVm -VM esx1Bot | Select -ExpandProperty OvfProperties</pre><p>And all the properties we saw earlier via the vApp Options page, are returned.</p>
<p><a href="https://www.lucd.info/2017/01/10/upgrade-appliance-ovftools/ovftools3/" rel="attachment wp-att-5524"><img loading="lazy" decoding="async" class="alignnone wp-image-5524 size-medium" src="https://www.lucd.info/wp-content/uploads/2017/01/ovftools3-300x194.png" width="300" height="194" srcset="https://www.lucd.info/wp-content/uploads/2017/01/ovftools3-300x194.png 300w, https://www.lucd.info/wp-content/uploads/2017/01/ovftools3.png 506w" sizes="auto, (max-width: 300px) 100vw, 300px" /></a></p>
<h3>Password property</h3>
<p>There is one kind of property that we can&#8217;t retrieve this way, these are the properties of type Password. To circumvent that issue, I have written the <strong>Update-OvfVm</strong> function in such a way that the user can provide these passwords via a separate parameter. Consider this a security measure.</p>
<p>In short, the function caller needs to created a <a href="https://msdn.microsoft.com/en-us/library/system.management.automation.pscredential(v=vs.85).aspx" target="_blank" rel="noopener noreferrer">PSCredential</a> object, where the <strong>User</strong> is the <strong>Name</strong> of the Ovf Password property.</p>
<p>First determine the password (1) properties, and find the propertyname (2).</p>
<p><a href="https://www.lucd.info/2017/01/10/upgrade-appliance-ovftools/ovftools4/" rel="attachment wp-att-5527"><img loading="lazy" decoding="async" class="alignnone wp-image-5527 size-medium" src="https://www.lucd.info/wp-content/uploads/2017/01/ovftools4-300x126.png" width="300" height="126" srcset="https://www.lucd.info/wp-content/uploads/2017/01/ovftools4-300x126.png 300w, https://www.lucd.info/wp-content/uploads/2017/01/ovftools4.png 414w" sizes="auto, (max-width: 300px) 100vw, 300px" /></a></p>
<p>In the script create a PSCredential object (3) where the username is equal to the propertyname (2). Pass the PSCredential object(s) on the call to the Update-OvfVm function (4).</p>
<p><a href="https://www.lucd.info/2017/01/10/upgrade-appliance-ovftools/ovftools5/" rel="attachment wp-att-5526"><img loading="lazy" decoding="async" class="alignnone size-full wp-image-5526" src="https://www.lucd.info/wp-content/uploads/2017/01/ovftools5.png" alt="" width="820" height="132" srcset="https://www.lucd.info/wp-content/uploads/2017/01/ovftools5.png 820w, https://www.lucd.info/wp-content/uploads/2017/01/ovftools5-300x48.png 300w, https://www.lucd.info/wp-content/uploads/2017/01/ovftools5-768x124.png 768w, https://www.lucd.info/wp-content/uploads/2017/01/ovftools5-720x116.png 720w" sizes="auto, (max-width: 820px) 100vw, 820px" /></a></p>
<p>The complete script to update the OpBot VM from v2.0 to v2.0.1 looks like this.</p><pre class="urvanov-syntax-highlighter-plain-tag">$ovfPath = 'C:\Temp\opbot_2.0.1\OpBot_2.0.1.ovf'
$vmName = 'esx1bot'
$suffix = '2.0.1'
$esxName = 'esx1*'
$dsName = 'DS1'

$credProperty = 'vmware_password'
$pswd = 'Whatever'

$secPswd = ConvertTo-SecureString -String $pswd -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential ($credProperty,$secPswd)

Get-OvfVm -VM $vmName | 
Update-OvfVm -VM $vmName -Path $ovfPath -Credential $cred -VMHost $esxName -Datastore $dsName -Suffix $suffix</pre><p>After the script is executed, the new version of the OpBot is deployed. The Ovf Properties are all copied. The switch from the old to the new appliance with some PowerCLI cmdlets should be trivial.</p>
<p><a href="https://www.lucd.info/2017/01/10/upgrade-appliance-ovftools/ovftools6/" rel="attachment wp-att-5531"><img loading="lazy" decoding="async" class="alignnone wp-image-5531 size-medium" src="https://www.lucd.info/wp-content/uploads/2017/01/ovftools6-300x127.png" width="300" height="127" srcset="https://www.lucd.info/wp-content/uploads/2017/01/ovftools6-300x127.png 300w, https://www.lucd.info/wp-content/uploads/2017/01/ovftools6-768x326.png 768w, https://www.lucd.info/wp-content/uploads/2017/01/ovftools6-720x305.png 720w, https://www.lucd.info/wp-content/uploads/2017/01/ovftools6.png 788w" sizes="auto, (max-width: 300px) 100vw, 300px" /></a></p>
<p>&nbsp;</p>
<p>Enjoy!</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.lucd.info/2017/01/10/upgrade-appliance-ovftools/feed/</wfw:commentRss>
			<slash:comments>4</slash:comments>
		
		
			</item>
	</channel>
</rss>
