Powers Combined: PowerCLI and OVFTool

"By your powers combined!!!"

"By your powers combined!!!"

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.

About these ads

9 Responses to “Powers Combined: PowerCLI and OVFTool”

  • Mark

    Hi Jake,

    The line 27 give me the following error:

    Get-View : 01-08-2012 01:08:32 Get-View View with Id ‘SessionManager
    -SessionManager’ was not found on the server(s).

    I am new to PowerCLI any ideas.

    Thanks,
    Mark

  • Chris Chapman

    Great Script! I had to add;

    $vm = Get-VM $vm

    before the line;

    $moref = $vm.extensiondata.moref.value

    or I got the error “Error: vmodl.fault.ManagedObjectNotFound”

    • Jake

      Hi Chris, you don’t need that if you pipe a VM object to the Export-VM function. I suppose I should put an example in the post. :D

      Like this:

       Get-VM MyVM | Export-VM -destination C:\users\me\Desktop\ 
  • bobbydamercer

    Hi

    Could you tell me what exactly is this ‘ Moref value ‘??
    like a Unique ID for the VM??

    TIA

  • Uploading VMs to vCloud with OVFtool « Geek after Five

    [...] more OVFtool fun, check out my post on using OVF tool with PowerCLI with your vSphere session! Share this:TwitterMoreFacebookDiggRedditStumbleUponLinkedInEmailPrintLike this:LikeBe the first to [...]

  • Daniel Engel

    Great article, gave me a good jump start. I’m using ESXi5.1 and things didn’t quite work as shown above. I had to do a few modifications:

    $moref = $vm.ExtensionData.MoRef.Value
    $session=get-view -Id $global:DefaultVIServer.ExtensionData.Client.ServiceContent.SessionManager
    $ticket = $session.AcquireCloneTicket() | Out-String
    & $ovftool “–I:sourceSessionTicket=$($ticket)” “–noSSLVerify” “vi://$($global:DefaultVIServer.name)?moref=vim.VirtualMachine:$($moref)” “$($destination)$($vm.name).ovf”

    Hope it helps others as well :)

  • Alan

    Thanks so much – VERY helpful, especially to a newb like me!
    For our ESXi 5.0 system this merge of your code with Daniel’s works:

    $vm = Get-VM $VMNAME
    $moref = $vm.ExtensionData.MoRef.Value
    $session=get-view -Id $global:DefaultVIServer.ExtensionData.Client.ServiceContent.SessionManager
    $ticket = $session.AcquireCloneTicket()
    & $ovftool “–I:sourceSessionTicket=$($ticket)” “vi://$($defaultviserver.name)?moref=vim.VirtualMachine:$($moref)” “$($destination)$($vm.name).ovf”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 957 other followers

%d bloggers like this: