Today we have another small script from the Quick and Dirty series. If you use VAAI-compatible storage and provision VMs thinly, you have to perform a space reclaim from time to time to be able to use the deleted VMFS blocks again.
In the VMware Knowledge Base describes the procedure using "esxcli", but I find the way via the PowerCLI somewhat more convenient and have therefore added a small text GUI:
All you have to do is start the script from the PowerCLI and then you can conveniently select the datastore on which the "UNMAP" command is to be executed.
The script works with the PowerCLI 6 and the EMC XtremIO and probably also with other VAAI compatible storage. The reclaim unit size can be adjusted in line 8. (Default is 200 for 1 MB blocks for VMFS3/5).
Here is the script:
function Reclaim-UnusedDatastoreSpace ($datastore, $ESXiHost) { try { write-host "Datastore Name: $datastore" write-host "ESX Server: $ESXiHost" $ESXCLI = Get-EsxCli -VMHost $ESXiHost $RETVAL = $ESXCLI.storage.vmfs.unmap(200, $datastore, $null) } catch [VMware.VimAutomation.Sdk.Types.V1.ErrorHandling.VimException.ViError] { } } $settimeout = Set-PowerCLIConfiguration -WebOperationTimeoutSeconds -1 -Scope Session -Confirm:$false clear-host write-host "" $vcenter = read-host "Enter vCenter Name" try { $connect = connect-viserver $vcenter } catch { write-host "Can't connect vCenter" exit 0 } do { $clusterlist = get-cluster $clusternumber = 1 $selectcluster = @() foreach ($cluster in $clusterlist) { $clustername = $cluster.name $selectcluster += new-object PSObject -property @{Number="$Clusternumber";Clustername="$Clustername"} $clusternumber++ } $selectcluster | ft number,clustername -autosize $selectedclusternumber = read-host "Select Cluster (Enter Number)" try { $selectedclustername = ($selectcluster | where {$_.Number -eq $selectedclusternumber} | select Clustername).Clustername $esxhost = get-cluster $selectedclustername | get-vmhost | select -first 1 } catch { write-host "Can't get ESX Server" } $datastorelist = get-vmhost $esxhost | get-datastore $datastorenumber = 1 $selectdatastore = @() foreach ($datastore in $datastorelist) { $datastorename = $datastore.name $selectdatastore += new-object PSObject -property @{Number="$Datastorenumber";DatastoreName="$datastorename"} $datastorenumber++ } $selectdatastore | ft Number,DatastoreName -autosize $selecteddatastorenumber = read-host "Select Datastore (Enter Number)" try { $selecteddatastorename = ($selectdatastore | where {$_.Number -eq $selecteddatastorenumber}).datastorename write-host "" write-host "Working..." write-host "" Reclaim-UnusedDatastoreSpace $selecteddatastorename $esxhost write-host "" write-host "Finished" -foregroundcolor green write-host "" $newrun = read-host "New run? (Yes/No)" } catch { write-host "Can't reclaim Space" } } while ($newrun -match "Yes" -or $newrun -match "y")