Migrate virtual machines and enable thin provisioning

I recently used this code to move a couple dozen terabytes of virtual machines from one array to another — all while enabling thin provisioning (aka sparse disk). Once the LUN migration is complete a nice little message shows up in your email. It is a very straight forward script that I thought was worth sharing.

[cc lang=”powershell”]
$startTime = get-date
$sourceDS = “array1-1000”
$destDS = “array2-3000”

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

Get-VM -Datastore $sourceDS | sort name | % {
$vmView = $_ | Get-View -Property Name
$spec = New-Object VMware.Vim.VirtualMachineRelocateSpec
$spec.datastore = $dsView.MoRef
$spec.transform = “sparse”
$vmView.RelocateVM($spec, $null)
}

Send-MailMessage -From “vmware@mydomain.local” -SmtpServer “smtp.mydomain.local” -Subject “Migration from $sourceDS to $destDS complete” -To “me@mydomain.local” -BodyAsHtml “This task started at $($startTime.datetime.tostring()) and finished at $((get-date).datetime.tostring())”
[/cc]

This entry was posted in Scripting, Virtualization. Bookmark the permalink.

One Response to Migrate virtual machines and enable thin provisioning

  1. Pingback: Emptying a VMFS datastore with PowerCLI | Christian's blog

Leave a Reply

Your email address will not be published. Required fields are marked *

*

Notify me of followup comments via e-mail. You can also subscribe without commenting.