Friday, 30 July 2010

Multi-VM Storage Migration Powershell Script

We had to migrate 100-odd VM's from Germany to London for a relocation, and were using a small HP MSA to do this work. Not wanting to take up overtime (which is money!) we automated the whole process via Powershell (and converted the VM's to sparse/thin disks at the same time).

Wrapped up into a function, the script takes input from a CSV that is structured as follows (can be exported using the "Get-VM" command):












We 'balanced the VM's, hence the "0" in the CSV (and provision for it in the script). The function is as simple as running the following on a machine with Powershell installed:

start powershell d:\clients\XXX\Migrate-Thin.ps1 Datastore_1.csv Datastore_1

The script loops through each VM in turn, keeping inside the Virtual Centre maximums on SVMotion jobs, and migrating them from the existing storage to the new. We did all 100-odd VM's overnight, shipped them to London and had them running again on the new storage within 48 hours, all thinly provisioned as well.


Function Migrate-Thin($CSVFile,$DataStoreName)


$FatVMList = Import-CSV $CSVFile

foreach ($FatVM in $FatVMList)


if ($FatVM.Name -ne "0")


connect-viserver MyVCCServer -User ChangeMe\MeToo -Password MyPassWord

$VM = Get-VM $FatVM.Name

$vmView = $VM | Get-View -Property Name

echo $vmView.Name

$dsView = Get-Datastore -Name $DataStoreName | Get-View -Property Name

$spec = New-Object VMware.Vim.VirtualMachineRelocateSpec

$spec.datastore = $dsView.MoRef

$spec.transform = "sparse"

$vmView.RelocateVM($spec, $null)

disconnect-viserver -confirm:$False




$RunningSnapins = Get-PSSnapin

$LoadVMSnapin = $True

foreach($Snapin in $RunningSnapins)


if($ -eq "VMware.VimAutomation.Core")


$LoadVMSnapin = $False



if($LoadVMSnapin -eq $True){

$AddTheSnapin = add-PSSnapin VMware.VimAutomation.Core


$CSV = $args[0]

$DS = $args[1]

Migrate-Thin $CSV $DS


Parikshith said...

Hi Andre, we have similar plans of making a data center move. so your script is of great value to us.How did you manage to SVmotion from Germany to London? Long distance VMotion??? if you shed some light on this it would be great.


Andre said...

Hi Parikshith

We used a small HP MSA2000 and had this sent via overnight courier. It was cheaper and faster than sending it via leased line however if you had enough 'grunt' in your connectivity you could do it over the internet.

Ho many VM's / total storage size are you looking to move? And what sort of connectivity do you have between your datacentres?

Post a Comment