Infrastructure as Code


vCloud PowerCLI SvMotion

06 Mar 2012

What's the correct capitalization supposed to be? sVmotion? SVMotion? SvMotion? Blah, whatever. The point is, William Lam wrote a couple killer posts on doing a storage vMotion through the vCloud API. Feeling the PowerCLI mafia needed a solution as well, I went ahead and wrote it. William's post about performing the vMotion with the REST API made writing the PowerCLI code very easy. A few general things to note:
  1. This is a storage vMotion for the entire VM. If you have VMs with multiple disks on different datastores, the theoretical behavior is that it will move all disks to the same datastore.
  2. You have to be a user with SYSTEM access. Normal Org admins don't have to worry about such things as datastores. It's all abstracted. :D
  3. You can't SvMotion to vCloud-disabled datastores. (I could think of some interesting use cases for that, but it doesn't work. ;))
  4. This is a great method to empty datastores for doing fresh VMFS volumes. Jason Boche has a great VMFS 5 article here. Don't forget to disable the datastore in vCloud Director before running! :D
$vmName = "MyVMName"
$destDatastoreName = "MyDatastoreName"

$vmQuery = Search-Cloud -QueryType AdminVM -Name $vmName

if ($destDatastoreName -eq $vmQuery.datastoreName)
 Write-Host -ForegroundColor Red "Silly Wabbit, You are trying to sVmotion to the same Datastore."
 $vm = Get-CIVM $vmName
 $dsQuery = Search-Cloud -QueryType Datastore -Name $destDatastoreName
 $dsRef = New-Object
 $dsRef.Href = "https://$($global:DefaultCIServers[0].name)/api/admin/extension/datastore/$($':')[-1])"
Lines 4-11: This is basically some validation to make sure we are not trying to migrate to the same datastore. Line 14: Get information on the datastore we are migrating to. Line 15: Create a reference object. Line 16: Search-Cloud only returns an ID, so we have to construct the href ourselves. (Has to do with the query type Seach-Cloud is using. No biggie.) Line 17: Relocate!
comments powered by Disqus