Mass SvMotion!

As part of my File Migration Series, I have been talking about moving files.  But this weekend I had to move a bunch of virtual machines from one storage array to another.  So I thought it was time for a script.  One of my co-workers – Will Urban – likes to script and he came up with something for me.  The script below will copy all virtual machines in one datastore to a new datastore, or all virtual machines in a folder to a new datastore.

So the script is below and you can copy and paste it and you are good to go.  You will be prompted for the appropriate info and by default the folder move will occur.

[code language=”powershell”]

##Mass Migrate VM Script
##Will Urban – Sept81014
##DataGravityInc
##Will incorporate scripts found online and end user query as well

##Variables can be added or user entered
##Comment out either function call depending on situation
$VCServer = Read-Host "Please enter the FQDN of the vCenter Server: "
$FromFolder = Read-Host "Please enter the folder that the VMs reside in: "
$FromDS = Read-Host "Please enter the name of the Datastore you want to move the VMs from: "
$ToDS = Read-Host "Please enter the name of the Datastore you want to move the VMs to: "

##Function Get-DSLatency – Credit Luc Dekens
##Finds the average latency for the datastore and throttles the number of Storage vMotions
function Get-DSLatency
{
param($dsName)

$ds = Get-Datastore -Name $dsName
$esx = Get-VMHost -Datastore $ds
$ds_uuid = $ds.ExtensionData.Info.Vmfs.Uuid
$metric = "datastore.sizeNormalizedDatastoreLatency.average"
$stats = Get-Stat -Entity $esx -Instance $ds_uuid -Stat $metric -Realtime -MaxSamples 1
($stats | Measure-Object -Average -Property Value).Average/1000
}

##Function Move-DSVM – Credit Luc Dekens
##Function call to move all the VMs from a certain datastore to another one
function Move-DSVM
{
param($fromDS, $toDS)

foreach($vm in (Get-VM -Datastore $fromDS)){
Move-VM -VM $vm -Datastore $toDS -RunAsync -Confirm:$false
while((Get-DSLatency -dsName $toDS) -gt 15){sleep 20}
}
}

##Function Move-FolderVM
##Function call to move all the VMs in a particular folder instead of the datastore
function Move-FolderVM
{
param($fromDS, $FromFolder, $toDS)

Write-Host $fromDS $FromFolder $toDS

foreach($vm in (Get-VM -Location $FromFolder)){
Move-VM -VM $vm -Datastore $fromDS -RunAsync -Confirm:$false
while((Get-DSLatency -dsName $toDS) -gt 15){sleep 20}
}
}

##Connect to Virtual Center
$Credential = Get-Credential
Connect-VIServer -Server bosvc01.pml.com -Credential $Credential

##Move VMs from the requested datastores
##########################Move-DSVM $FromDS $ToDS

##Move VMs from the requested folder
Move-FolderVM $FromDS $FromFolder $ToDS

##Disconnect when done, ignore prompt
Disconnect-VIServer -Server * -Force -Confirm:$false

[/code]

You can see above the ##Move VMs from requested datastores section.  It is completely remarked out so that means you get the folder move.

##Move VMs from the requested datastores
##########################Move-DSVM $FromDS $ToDS
##Move VMs from the requested folder
Move-FolderVM $FromDS $FromFolder $ToDS

If you un – remark datastores, and remark the requested folder option you will get the datastore move.

It would look like this:

##Move VMs from the requested datastores
Move-DSVM $FromDS $ToDS
##Move VMs from the requested folder
#############Move-FolderVM $FromDS $FromFolder $ToDS

I hope that this helps!  I can tell you that the datastore move works really well as that is what I used – there is a latency functionality in this script so nothing was overwhelmed!  Let me know how it goes – and thanks to Will!

Update – 10/9/14 – After upgrading to PowerCLI 5.8 R2 I have an issue with this script.  Now the latency part of it doesn’t work due to an issue with Get-stat.  It looks like this:

IssueGet-Stat

The migration still works but you see this error.  What is very important to note is that without this latency type functionality all of the migrations start at the same time.    So not good.  However vCenter will not let them all proceed at once.  It will limit them to minimize impact.  I will update as soon as I figure this out.

Michael

=== END ===

Leave a Reply