White space removal script at ESX level
If you have some storage space issue and you use still VMFS5 format please use below script to reclaim storage space.
ESXi supports the unmap commands issued directly from a guest operating system to reclaim storage space. The command helps thin-provisioned storage arrays to reclaim unused space from the VMFS datastore and thin virtual disks on the datastore. For example, you delete files on the thin virtual disk. The guest operating system notifies VMFS about freed space by sending the unmap command. The unmap command sent from the guest operating system releases space within the VMFS datastore. The command then proceeds to the array, so that the array can reclaim the freed blocks of space.
The VMFS6 datastore can send the space reclamation command automatically. With the VMFS5 datastore, you can manually reclaim storage space.
array can reclaim the freed blocks of space.
SCRIPT>> ReclaimSpace-ClusterScript
$Location = “S:\Scripts\UnMap”
Set-Location -Path $Location
$Date = Get-Date -Format “dd-MMM-yyyy hh-mm tt”
$LogFile = $Location + “\Logs\SYDDR-UnMapLog – $Date.txt”
function LogMessage ([String[]] $messages)
{
$timestamp = $([DateTime]::Now).ToString()
foreach ($message in $messages)
{
if ([string]::IsNullOrEmpty($message))
{
continue
}
Write-Host “$timestamp $message”
$message = $message.Replace(“`r`n”, ” “)
$message = $message.Replace(“`n”, ” “)
Add-Content $LogFile “$timestamp $message”
}
}
#Importing the VMware Automation Module
Import-Module -Name “Vmware.VimAutomation.core”
LogMessage “—-Setting the WebOperation Time out Value for this session—-”
Set-PowerCLIConfiguration -WebOperationTimeoutSeconds -1 -Scope Session -Confirm:$false
$ESXiHost = “Enter the ESXi hosts name”
$Rest = “30”
$Blocks = “300”
LogMessage “——Sorting Credentials————–”
#Created a credential file to login to one of the SYD DR Clsuter server StorageCluster001. Below command is importing the credential file to login to ESXi host.
$Credential = Import-Clixml -Path S:\Scripts\UnMap\Credentials\StorageCluster001.cred
LogMessage “——Connecting to the ESXi Host——”
Connect-VIServer -Server $ESXiHost -Credential $Credential
LogMessage “——–Connected to $ESXiHost ——–”
LogMessage “—————————————”
Send-MailMessage -From StorageUnmapScript-SYD-DR-Cluster@aon.com -To vmlabtest@gmail.com,test@gmail.com -Subject “Storage Unmap Script execution Started on SYD DR Cluster – $ESXiHost” -BodyAsHtml “The script has been executed from $env:COMPUTERNAME on $Date by $env:USERNAME.” -SmtpServer smtprelayap.aon.net
LogMessage “—-Starting UnMap on ESXi Host $ESXiHost—-”
LogMessage “—————————————”
$HostESXCli = Get-EsxCli -VMHost $ESXiHost
$DataStores = Get-Datastore -VMHost $ESXiHost | Where-Object {$_.ExtensionData.Summary.Type -eq ‘VMFS’ -and $_.ExtensionData.Capability.PerFileThinProvisioningSupported} | Sort-Object Name
foreach($DStore in $DataStores)
{
LogMessage “——————————————-”
LogMessage “—-Starting UnMap on Datastore $DStore—-”
LogMessage “——————————————-”
try
{
$HostESXCli.storage.vmfs.unmap($Blocks, “$DStore”, $null)
LogMessage ” ———————————————————— ”
LogMessage ” — Unmap has completed on DataStore $DStore — ”
LogMessage ” ———————————————————— ”
}
Catch
{
LogMessage $_
}
Start-Sleep -s $Rest
}
LogMessage “—-Script Execution has been completed—-”
Send-MailMessage -From StorageUnmapScript-SYD-DR-Cluster@aon.com -To vmlabtest@gmail.com,test@gmail.com -Subject “Storage Unmap Script execution Completed on SYD DR Cluster – $ESXiHost” -BodyAsHtml “The script has been executed from $env:COMPUTERNAME on $Date by $env:USERNAME.” -Attachments $LogFile -SmtpServer smtprelayap.aon.net
LogMessage “—-Disconnecting the Host $ESXiHost —-”
Disconnect-VIServer -server $ESXiHost -Confirm:$false