Monday, January 23, 2012

Powershell SVmotion Move VM one HD at a time

When i had to move some rather big vm's with 5-15 HD's and a size of 3-6TB attached, I wanted to ensure the vm didn't get corrupt. SO i wondered f it was possible to move vm's one HD at a time. and yes it is. but previously you had to do it via some very tricky calls to the api now its just a cmd-let that handles it.

Param($Name="",$Datastore="")
$vm= get-vm $name
if (($vm) -and (get-datastore $datastore)) {
$Harddisks = Get-HardDisk -vm $vm
Foreach ($hd in $harddisks) {
       Set-HardDisk -HardDisk $hd -Datastore $datastore -Confirm:$false
}
$vm | move-vm -datastore $datastore -Confirm:$false
}

The script explained. first i find the vm, and the target datastore. then i step through the HD's one by one and use the set-harddisk cmd-let to place the HD on the target datastore. at the end i move the remainig part of the vm thats the vmx file, logs and so to the target drive.

This was done on ESX5i,  vcenter5.0 and PowerCLI 5.0