VMware UNMAP Script

VMware ESXi 6.7 öncesi sunucular üzerindeki thinprovision lun’lardaki dataların silinmesi sonrasında, lun’lardaki değişikliği storage’a geri kazandırılması için kullanılabilecek script.

Clear-Host
$ErrorActionPreference = "Continue"
$DebugPreference = "Continue"
$VerbosePreference = "Continue"


$VCServer = "vcenter01"
Write-Verbose "Connected to '$VCServer'"


if ((Get-PSSnapin -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue) -eq $null) {
     Add-PSSnapin VMware.VimAutomation.Core      }

Set-PowerCLIConfiguration -WebOperationTimeoutSeconds -1 -Scope Session -Confirm:$false
Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Scope Session -Confirm:$false

$VC = Connect-VIServer $VCServer
Write-Verbose "Connected to '$($VC.Name):$($VC.port)' as '$($VC.USER)'"

Get-Cluster
$ClusterName = 'VDI Cluster'
Get-Cluster -Name $Clustername | Get-VMHost | Sort-Object -Property MemoryUsageGB
$esx = Get-Cluster -Name $Clustername | Get-VMHost | Sort-Object -Property MemoryUsageGB | Select -First 1
$esxcli = Get-EsxCli -VMHost $esx
Write-Verbose "Using ESXi Host '$($esx)' for CLI'"

Get-Datastore -Name "3PAR*"

$DS1 = '3PAR_14111_01'
Write-Verbose "Queried '$DS1'"
$DS2 = '3PAR_34523_02'
Write-Verbose "Queried '$DS2'"
$DS3 = '3PAR_23535_03'
Write-Verbose "Queried '$DS3'"
$DS4 = '3PAR_24223_04'
Write-Verbose "Queried '$DS4'"
$DS5 = '3PAR_25423_05'
Write-Verbose "Queried '$DS5'"
$DS6 = '3PAR_23452_06'
Write-Verbose "Queried '$DS6'"
$DS7 = '3PAR_52345_07'
Write-Verbose "Queried '$DS7'"
$DS8 = '3PAR_23453_08'
Write-Verbose "Queried '$DS8'"
$DS9 = '3PAR_67856_09'
Write-Verbose "Queried '$DS9'"
$DS10 = '3PAR_58542_10'
Write-Verbose "Queried '$DS10'"

Write-Verbose "Starting UnMaP.."

Write-Verbose "Processing '$DS1'"
$esxcli.storage.vmfs.unmap($null, $DS1, $null)
Write-Verbose "Processing '$DS2'"
$esxcli.storage.vmfs.unmap($null, $DS2, $null)
Write-Verbose "Processing '$DS3'"
$esxcli.storage.vmfs.unmap($null, $DS3, $null)
Write-Verbose "Processing '$DS4'"
$esxcli.storage.vmfs.unmap($null, $DS4, $null)
Write-Verbose "Processing '$DS5'"
$esxcli.storage.vmfs.unmap($null, $DS5, $null)
Write-Verbose "Processing '$DS6'"
$esxcli.storage.vmfs.unmap($null, $DS6, $null)
Write-Verbose "Processing '$DS7'"
$esxcli.storage.vmfs.unmap($null, $DS7, $null)
Write-Verbose "Processing '$DS8'"
$esxcli.storage.vmfs.unmap($null, $DS8, $null)
Write-Verbose "Processing '$DS9'"
$esxcli.storage.vmfs.unmap($null, $DS9, $null)
Write-Verbose "Processing '$DS10'"
$esxcli.storage.vmfs.unmap($null, $DS10, $null)


Disconnect-VIServer $VCServer -Confirm:$false

Write-Verbose "Script Ended"


<#
.DESCRIPTION
	This script connects to an ESXi host and runs an UNMAP against each datastore to reclaim space from thin file operations.
	Usage: AutoUnmap.ps1 ESXiHostname
	Requires ESXi version >= 5.5 &amp; PowerCLI 6.5 R1
.NOTES
  	Version:        1.0
  	Author:         Brett Sinclair
  	Github:         http://www.github.com/brett-sinclair
  	Twitter:        @Pragmatic_IO
  	Website:        http://www.pragmaticio.com
#>
Param
(
   [Parameter(Mandatory=$true)]
   $HostName
)
 
$User = "your_username"                            
$Password = "yourpassword"                 
$Rest = "300"                         
#below sets the vmfs block per iteration value. Default is 200
$blocks = "300"
 
#ensure snap in is loaded so PS execution is possible and remove any cert warnings
If ((Get-PSSnapin -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue) -eq $null ) { 
Add-PSSnapin VMware.VimAutomation.Core }
Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -confirm:$false
 
#it's go time 
Connect-VIServer -Server $HostName -user $User -password $Password  
$HostEsxCli = Get-EsxCli -VMHost $HostName
$DataStores = Get-Datastore | Where-Object {$_.ExtensionData.Summary.Type -eq 'VMFS' -And $_.ExtensionData.Capability.PerFileThinProvisioningSupported} | Sort-Object Name
ForEach ($DStore in $DataStores) { 
    Write-Host " ------------------------------------------------------------ " -ForegroundColor 'yellow'
    Write-Host " -- Starting Unmap on DataStore $DStore -- " -ForegroundColor 'yellow' 
    Write-Host " ------------------------------------------------------------ " -ForegroundColor 'yellow'
    $HostEsxCli.storage.vmfs.unmap($blocks,"$DStore", $null)
    Write-Host " ------------------------------------------------------------ " -ForegroundColor 'green'
    Write-Host " -- Unmap has completed on DataStore $DStore -- " -ForegroundColor 'green'
    Write-Host " ------------------------------------------------------------ " -ForegroundColor 'green'
    Start-Sleep -s $Rest
}