Blog

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>
<div></div>
<div>#Variables
$vcenterName = "vCenter name"
$smtpsrv = "smtp server
$fromAddr = "SnapshotReport@snapshot.com"
$toAddr = "destination address"
$attachmentPref = $True</div>
<div></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></div>
<div>#Connect to VCenter Server
Connect-VIServer -Server $vcenterName #-Username $vcusr -Password $vcpsd</div>
<div></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></div>
<div>#Exit if no snapshots
$VMsWithSnapscount = ($VMsWithSnaps | measure-object).count
if($VMsWithSnapscount -eq 0){write-host 'No Snapshots found';break}</div>
<div></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></div>
<div>    $body += "</table></center>"
# Send email alerting recipients about snapshots.
if($attachmentPref){
$csvfile = $env:temp + '\SnapshotReport_' + (Get-Date).ToString("yyyy-MM-dd_HH.mm") + '.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
}
Else{
Send-MailMessage -To $toAddr -From $fromAddr -Subject "WARNING: VMware Snapshots found on $date" -Body "$body" -SmtpServer "$smtpsrv" -BodyAsHtml
}</div>
<div></div>
<div>#Disconnect from VCenter Server
Disconnect-VIServer -Server $vcenterserver -Confirm:$false</div>
<div>}

Leave a Comment

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