PowerCLI script to execute UNMAP on VMFS LUNs to reclaim disk space
# ./unmap.ps1 ToMail@mailaddress.com
#### HTML Output Formatting #######
$a = "<style>"
$a = $a + "BODY{background-color:White ;}"
$a = $a + "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}"
$a = $a + "TH{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:Green}"
$a = $a + "TD{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:PaleGoldenrod}"
$a = $a + "</style>"
################################################################################################
#Remove-Item C:\Temp\UnmapInfo.html
Connect-VIServer vcenterserver
Set-PowerCLIConfiguration -Scope Session -WebOperationTimeoutSeconds 1800000
$ToMail=$args[0]
$input = Import-Csv C:\Temp\dslist.csv
$report = @()
$done = 'Unmap Done'
foreach ($ds in $input)
{
$row = "" | select DSName,Status
$esxName=$ds.ESXiHost
$row.DSName=$ds.DSName
$esxcli=Get-EsxCli -VMHost $esxName -V2
$sParam = @{
volumelabel = $row.DSName
}
$result=$esxcli.storage.vmfs.unmap.Invoke($sParam)
if($result -eq 'true') {$row.Status=$done}
if($result -ne 'true') {$row.Status=$result}
$report += $row
}
$report | Sort DSName | ConvertTo-html -Head $a -Body "<H2> </H2>" >> C:\Temp\UnmapInfo.html
$body = [System.IO.File]::ReadAllText('C:\Temp\UnmapInfo.html')
$text = '<b><font=Arial>UNMAP Status Report</font></b>'
Send-MailMessage -To $ToMail -From FromMail@mailaddress.com -Subject "UNMAP Status Report" -Body "$text $body" -SmtpServer smtp.mailaddress.com -BodyAsHtml
Disconnect-VIServer vcenterserver -Confirm:$false
<#
Content of dslist.csv
CurrentDSName,ModifiedDSName
ESXiHost1,DS01
ESXiHost2,DS02
ESXiHost3,DS03
#>
Hi I did not understand dslist.csv content. Is it changing the names of ds? What my requirement is to reclaim dead space from each datastores. It should not unmount luns/datastores and process should run for each object/dasatores
ReplyDelete