Tuesday, October 19, 2021

Snapshot report using PowerCLI script

 Snapshot report using PowerCLI script


#### HTML Output Formatting #######

$a = "<style>"

$a = $a + "BODY{font-family:Calibri;font-size:12pt;}"

$a = $a + "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse; padding-right:5px}"

$a = $a + "TH{border-width: 1px;padding: 5px;border-style: solid;border-color: black;color:black;background-color:#FFFFFF }"

$a = $a + "TD{border-width: 1px;padding: 5px;border-style: solid;border-color: black;background-color:Silver}"

$a = $a + "</style>"


Connect-VIServer vcenterserver

Remove-Item D:\temp\SnapshotReport.html

$CurrentDate = Get-Date

$CurrentDate = $CurrentDate.ToString('dd-MMM-yyyy')

$HeadLines = "<BR><i>Snapshot Report - $CurrentDate </i>"


$post = "<BR><i>Snapshots older than 10 Days as of $CurrentDate </i>"

Get-VM | ?{$_.PowerState -eq "PoweredOn"} | Get-Snapshot | Where-Object {$_.Created -lt (Get-Date).AddDays(-10)} | Select-Object VM, Description, Created, @{Label=”Age”;Expression={(New-TimeSpan -Start $_.Created -End (Get-Date)).Days}}, @{Label=”SizeGB”;Expression={“{0:N2} GB” -f ($_.SizeGB)}} | Sort-Object "Age" -Descending | ConvertTo-html -Head $a -PreContent $post -Body "<H2> </H2>" >> D:\temp\SnapshotReport.html


$body = [System.IO.File]::ReadAllText('D:\temp\SnapshotReport.html')

Send-MailMessage -To To@Mailaddress.com -From From@Mailaddress.com -Subject "Snapshot Report - $CurrentDate" -Body $body -SmtpServer smtp.Mailaddress.com -BodyAsHtml

Disconnect-VIServer vcenterserver -Confirm:$False