Quantcast
Channel: dbailey's blog
Viewing all articles
Browse latest Browse all 8

Advanced Storage VMotions via PowerShell

0
0

When you want to migrate a virtual machine in the vCenter Client, the wizard gives you the option to click the Advanced button and then select the destinations for the configuration file and each hard disk independently.

Advanced VM migration Wizard

Unfortunately, the Move-VM PowerCLI cmdlet only allows you to perform a “basic” move where all the VM files reside on one datastore.

I wanted a way to script the advanced move and stumbled across a post on the VMware forums that lead me down the right path.

Basically, you define a VMware.Vim.VirtualMachineRelocateSpec object that tells Virtual Center where everything should go. We’re primarily interested in two members of this type. The first is Datastore. Datastore is the MoRef of the destination datastore for the configuration files and any VMDKs for which no destination is specified. The second member is Disk. Disk is an array of VMware.Vim.VirtualMachineRelocateSpecDiskLocator objects. These define where each hard disk will move. RelocateSpecDiskLocator has a DiskId member and a Datastore member. DiskId is the identifier of the VMDK internal to the VM and Datastore is again the MoRef of the destination datastore.

To perform an advanced move, you simply contruct your relocate spec objects and call .ExtensionData.RelocateVM_Task() on the VM object you want to move.

I wrote a function that takes the VM and an array of destination datastores to perform advanced moves. The first item in the destination array is the destination of the configuration files and each subsequent item is the destination of the VMDKs in order. If you specify the destination “current”, the function leaves that item in its current location.

So, for example, you can run:

Move-VMAdvanced -VM myVM -Datastores datastore01,datastore02,datastore03

This  moves the configuration files to datastore01 , the first VMDK to datastore02, and the second VMDK to datastore03. Note that separating the configuration files and the first VMDK is pretty rare and probably shouldn’t be done but this illustrates what you can do.

A more typical example would be:

Move-VMAdvanced -VM myVM -Datastores current,current,datastore03

This moves the second VMDK file to datastore03 while leaving the configuration files and the first VMDK in their current locations.

The function returns an object with the the name of the VM and the task ID. That way, you can pass the task to “Get-Task -ID” and monitor the progress with other bits of code.

All of this was written with vSphere 5 in mind. It may work on different versions but I haven’t tested it beyond that. Also, I haven’t made any considerations or tested with RDMs, thin provisioned disks or other hard disk types yet.

You can grab the function at Move-VMAdvanced.ps1


Viewing all articles
Browse latest Browse all 8

Latest Images

Trending Articles





Latest Images