In an earlier post I mentioned a script written by a co-worker to determine all of the non-thinprovisioned virtual machines. This script is a follow-up to that, which allows actually uses storage vMotion to resolve the problems. It is important to note that this script does not provide ANY error checking — if you try to move virtual machines to a datastore that does not have enough capacity you’ll be in trouble. However, I have used this script to re-arrange many virtual machines and it was successful (I always verified I had enough target capacity before starting the script).
[cc lang=”powershell”]$dsView = Get-Datastore -Name “target-datastore” | Get-View -Property Name
Get-VM “vm-num1″,”vm-num2” | % {
$vmView = $_ | Get-View -Property Name
$spec = New-Object VMware.Vim.VirtualMachineRelocateSpec
$spec.datastore = $dsView.MoRef
$spec.transform = “sparse”
$vmView.RelocateVM($spec, $null)
}[/cc]