Powercli script – Find snapshots and send email

This is a great script that I run frequently for customers to find snapshots in their environment and try to get them to remove them given the trouble prolonged snapshots can cause. It will interrogate the environment hold the inventory required information on VM snapshots and then output into an email in a table format..

In this format it will prompt you for a username and password but if you don’t mind having a password in a text doc you can add variables to include that.


# VMWare Snapshts Report</div>
$vcenterName = "vCenter name"
$smtpsrv = "smtp server
$fromAddr = ""
$toAddr = "destination address"
$attachmentPref = $True</div>
<div>#Add Snapin
$CheckSnapin = $null
$CheckSnapin = GET-PSSnapin vmware* –erroraction SilentlyContinue
if($CheckSnapin -eq $null ){Add-PSSnapin VMware.VimAutomation.Core –erroraction SilentlyContinue}
$CheckSnapin = $null
$CheckSnapin = GET-PSSnapin vmware* –erroraction SilentlyContinue
if($CheckSnapin -eq $null -and $CheckSnapin2012 -eq $null){write-host "VMware Snapin not loaded" -fore red}
#Ignore invalid Certs
Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$False</div>
<div>#Connect to VCenter Server
Connect-VIServer -Server $vcenterName #-Username $vcusr -Password $vcpsd</div>
<div>#Obtain VMWare Snapshots
$reporttime = get-date
$VMsWithSnaps = Get-VM | Get-Snapshot | Select vm,name,@{Name='created'; expression={$_.created.ToString("yyyy-MM-dd HH:mm:ss")}},@{Name='DaysOld'; expression={[math]::round(($reporttime - $_.created).TotalDays,0)}},@{Name='sizemb'; expression={[math]::round($_.sizemb,0)}} |sort-object created</div>
<div>#Exit if no snapshots
$VMsWithSnapscount = ($VMsWithSnaps | measure-object).count
if($VMsWithSnapscount -eq 0){write-host 'No Snapshots found';break}</div>
<div>#Send Email
$date = Get-Date -Format "dd/MM/yyyy"
$body = @("
<center><table border=1 width=100% cellspacing=0 cellpadding=2 bgcolor=Black cols=3>
<tr bgcolor=White><td>Virtual Machine</td><td>Snapshot</td><td>Created Date</td><td>Age (Days)</td><td>Size in MB</td></tr>")
$i = 0
do {
if($i % 2){$body += "<tr bgcolor=#D2CFCF><td>$($VMsWithSnaps[$i].VM)</td><td>$($VMsWithSnaps[$i].Name)</td><td>$($VMsWithSnaps[$i].Created)</td><td>$($VMsWithSnaps[$i].DaysOld)</td><td>$($VMsWithSnaps[$i].Sizemb)</td></tr>";$i++}
else {$body += "<tr bgcolor=#EFEFEF><td>$($VMsWithSnaps[$i].VM)</td><td>$($VMsWithSnaps[$i].Name)</td><td>$($VMsWithSnaps[$i].Created)</td><td>$($VMsWithSnaps[$i].DaysOld)</td><td>$($VMsWithSnaps[$i].Sizemb)</td></tr>";$i++}
while ($VMsWithSnaps[$i] -ne $null)</div>
<div>    $body += "</table></center>"
# Send email alerting recipients about snapshots.
$csvfile = $env:temp + '\SnapshotReport_' + (Get-Date).ToString("") + '.csv'
$VMsWithSnaps | Export-CSV $csvfile -notype
Send-MailMessage -To $toAddr -From $fromAddr -Subject "WARNING: VMware Snapshots found on $date" -Body "$body" -Attachments $csvfile -SmtpServer "$smtpsrv" -BodyAsHtml
Remove-Item $csvfile
Send-MailMessage -To $toAddr -From $fromAddr -Subject "WARNING: VMware Snapshots found on $date" -Body "$body" -SmtpServer "$smtpsrv" -BodyAsHtml
<div>#Disconnect from VCenter Server
Disconnect-VIServer -Server $vcenterserver -Confirm:$false</div>

Leave a Comment

Your email address will not be published. Required fields are marked *