
"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.
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.


July 31st, 2012 at 3:46 pm
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
July 31st, 2012 at 11:49 pm
No worries, Mark. Did you connect first with this?
If so, how many vCenter servers are you connected to?
August 2nd, 2012 at 10:01 am
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”
August 2nd, 2012 at 10:11 am
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.
Like this:
August 2nd, 2012 at 6:52 pm
Hi
Could you tell me what exactly is this ‘ Moref value ‘??
like a Unique ID for the VM??
TIA
August 2nd, 2012 at 6:55 pm
You are correct. The ‘moref’ or Managed Object Reference is a unique identifier for the virtual machine.
August 8th, 2012 at 5:48 pm
[...] 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 [...]
November 7th, 2012 at 3:17 pm
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
January 21st, 2013 at 1:13 pm
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”