GeekAfterFive

Infrastructure as Code

home

Powers Combined: PowerCLI and OVFTool

07 Oct 2011

[caption id="attachment_127" align="alignleft" width="249" caption=""By your powers combined!!!""][/caption] This post will show how to combine the automation abilities of PowerCLI with OVFTool, but first: a bit of history... Open Virtualization Format (OVF) is designed to be a "universal" packaging system for virtual machines (and vApps, for that matter) across any virtualization platform. While adoption is still happening, it is already VERY useful for importing and exporting across your VMware environments. VMware has an excellent command-line OVF Tool called...uh...OVFTool. Arne has a great post about using OVFTool in your environment. The problem... While OVF is a great standard, there can be issues (encoding, unsupported metadata, OVF version mismatches) when importing and exporting to/from different platforms. We're going to talk specifically about the Hybrid Cloud use case: vSphere to vCloud Director. PowerCLI has a pretty good method for exporting vApps (Export-vApp), but not Virtual Machines. The simple and effective work-around is to simply create a new vApp, move/clone your VM to the vApp, export the vApp using the Export-vApp cmdlet. It works great for vSphere->;vSphere, but again...not for vSphere->;vCloud.
New-VApp test02 -Location cluster01
Move-VM delta -Destination test02
Export-VApp test02 -Destination C:\users\jake\desktop
Uploading the OVF produced by the code above results in the following error: Easy Fix? Use OVFtool. It uploads perfectly to vCloud AND I can directly export the VM without the need for a vApp! "But wait a tick..." you say. "That's a command-line tool, not a PowerCLI cmdlet! I'll have to have to pass login credentials to OVFtool somehow." Exactly. We're going to use our PowerCLI session and a somewhat secret and cool feature in OVFTool to automate the export of our VM. So here's the code:
function Export-VM
{
    param
    (
        [parameter(Mandatory=$true,ValueFromPipeline=$true)] $vm,
        [parameter(Mandatory=$true)][String] $destination
    )

    $ovftoolpaths = ("C:\Program Files (x86)\VMware\VMware OVF Tool\ovftool.exe","C:\Program Files\VMware\VMware OVF Tool\ovftool.exe")
    $ovftool = ''

    foreach ($ovftoolpath in $ovftoolpaths)
    {
        if(test-path $ovftoolpath)
        {
            $ovftool = $ovftoolpath
        }
    }
    if (!$ovftool)
    {
        write-host -ForegroundColor red "ERROR: OVFtool not found in it's standard path."
        write-host -ForegroundColor red "Edit the path variable or download ovftool here: http://www.vmware.com/support/developer/ovf/"
    }
    else
    {
        $moref = $vm.extensiondata.moref.value
        $session = Get-View -Id SessionManager
        $ticket = $session.AcquireCloneTicket()
        & $ovftool "--I:sourceSessionTicket=$($ticket)" "vi://$($defaultviserver.name)?moref=vim.VirtualMachine:$($moref)" "$($destination)$($vm.name).ovf"
    }
}
Lines 26-29 are where the magic happens. Line 26: Get our Moref value for the VM. Line 27: Get our PowerCLI session data Line 28: Acquire a "clone ticket" for our session Line 29: Run OVFTool with our session ticket, moref value, and the destination parameter. Four lines of code is all it takes, just like Export-vApp. :D And the best part is uploading to vCloud works: So, in summary: You can combine the forces of PowerCLI and OVFTool to automate exporting of VMs, perfectly encoded for vCloud uploads. Stay tuned for part two, where we will use the upcoming vCloud PowerCLI snapin to fully automate VM migration to the public cloud.
comments powered by Disqus