Well, once again I hacked at the Powershell/PowerCLI the other day. Since we don’t yet have a Enterprise Plus license at work (which would support Datastore Maintaince and Storage DRS), I needed a way to empty one datastore and move all the content into another one, while enabling Thin-Provisioning.

So I googled for a bit, and actually found a few hints … So without further yada-yada, here’s the script I came up with:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
param( [string] $vCenter, [string] $datastore_old, $datastore_new )

# Add the VI-Snapin if it isn't loaded already
if ( (Get-PSSnapin -Name "VMware.VimAutomation.Core" -ErrorAction SilentlyContinue) -eq $null )
{
	Add-PSSnapin -Name "VMware.VimAutomation.Core"
}

if ( !($vCenter) -or !($datastore_old) -or !($datastore_new) ) {
	Write-Host `n "datastore-massmigrate-vms: <vcenter> <datastore_old> <datastore_new>" `n
	Write-Host "   datastore-massmigrate-vms moves all VMs residing on <datastore_old>"
	Write-Host "   to the datastore named <datastore_new>." `n
	exit 1
}

# Turn off Errors
$ErrorActionPreference = "silentlycontinue"

Connect-VIServer -Server $vCenter

Write-Host "Migrating VMs from datastore" $datastore_old "to" $datastore_new

$vms = Get-VM -datastore $datastore_old

foreach ($vm in $vms) {
         Write-Host "Moving" $vm "from" $datastore_old "to" $datastore_new
         Move-VM $vm -Datastore $datastore_new
}

Disconnect-VIServer -server $vCenter -Confirm:$false

Initially I have something different, which was a bit shorter (based on Brian’s work), but it had a huge downside: If you have vRDMs (like I do), it converts every damn vRDM into a VMDK, so make sure you only run this script on a datastore on VMs that either have only pRDMs or VMs with only VMDKs.