Generate Detailed Snapshots Report using PowerCLI Scripting

According to the guidelines provided by VMWare, it is strongly advised not to keep a VM snapshot for more than 72 hours.

Doing so can lead to issues like increased storage usage on the datastore, potential data loss, or data corruption. The size of the VM snapshot file grows over time, and eventually, it can consume all available storage space.

Managing snapshots becomes particularly challenging when dealing with a large infrastructure with hundreds or thousands of virtual machines. In such cases, a snapshot report script proves to be a highly useful tool.

In this blog post, I have developed a script by combining information from various sources and customizing it for my specific needs.

The script enables you to generate a snapshot report and receive it directly in your email.

Usage:

.\SnapshotsReport.ps1 -vcenter vcsa.local -username * -password * -email youremail@gmail.com

Modify in the script:

  • SMTP server ( required )
  • Title ( optional )
  • Mail content ( optional )


Script

#requires -modules VMware.VimAutomation.Core

param (
    [CmdletBinding()]
    [Parameter(Mandatory = $true)]
    [string]$VCenter,
     
    [Parameter(Mandatory = $true)]
    [string]$Username,
     
    [Parameter(Mandatory = $true)]
    [string]$Password,
     
    [Parameter(Mandatory = $false)]
    [string]$Email
)
 
Import-Module -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue
Set-PowerCLIConfiguration -ProxyPolicy NoProxy -DefaultVIServerMode multiple -Scope User -InvalidCertificateAction ignore -Confirm:$false | Out-Null
$ScriptDir = Split-Path $script:MyInvocation.MyCommand.Path
 
# Connection to vCenter
try {
    Connect-VIServer -Server $VCenter -User $Username -Password $Password -ErrorAction Stop | Out-Null
}
catch [VMware.VimAutomation.ViCore.Types.V1.ErrorHandling.InvalidLogin] {
    Write-Host "Cannot connect to $VCenter with provided credentials" -ForegroundColor Red
    Continue
}
catch [VMware.VimAutomation.Sdk.Types.V1.ErrorHandling.VimException.ViServerConnectionException] {
    Write-Host "Cannot connect to $VCenter - check IP/FQDN" -ForegroundColor Red
    Continue
}
catch {
    Write-Host "Cannot connect to $VCenter - Unknown error" -ForegroundColor Red
    Continue
}
 
$Snapshots = Get-VM | Sort | Get-Snapshot | Select-Object @{n="VCenter"; e={$VCenter}}, VM, @{n="SnapshotState"; e={$_.PowerState}}, Name, Description, @{Name="SizeGB";Expression={ [math]::Round($_.SizeGB,2) }}, Created, @{Name="DaysOld";Expression={(New-TimeSpan -End (Get-Date) -Start $_.Created).Days }}
$Snapshots | Export-Csv "$ScriptDir\SnapshotReport $VCenter.csv" -NoTypeInformation
 
Disconnect-VIServer -Server $VCenter -Confirm:$false
 
if (!$Email) {
    #### VARIABLES #######
    $ReportTime = Get-Date -Format yyyy.M.d
    $MailTitle = "Snapshot Report"
 
    #### Attachment file ####
    $AttachmentFile = "$ScriptDir\SnapshotReport $VCenter.csv"
 
    $HowManySnapshots = ($Snapshots.VM).Count
 
    ###############################
    # HTML Template
    $EmailBody = @"
<style>
H1 {
  font-size: 16px;
}
H2 {
  font-size: 14px;
}
H3 {
  font-size: 12px;
}
.content-table {
  border: 1px solid #0074b0;
  margin: 2px 0;
  font-size: 0.9em;
  border-radius: 5px 5px 0 0;
}

.content-table thead tr {
  background-color: #0074b0;
  color: #ffffff;
  text-align: left;
  font-weight: bold;
}

.content-table th,
.content-table td {
  border: 1px solid #0074b0;
  padding: 2px 5px;
}

.content-table tbody tr {
  border-bottom: thin solid #dddddd;
}

.content-table tbody tr:nth-of-type(even) {
  background-color: #f3f3f3;
}

.content-table tbody tr:last-of-type {
  border-bottom: thin solid #0074b0;
}

.content-table tbody tr.active-row {
  font-weight: bold;
  color: #0074b0;
}

.content-table tbody td.failed-row {
  font-weight: bold;
  color: #ff0000;
}

.content-table tbody td.succ-row {
  font-weight: bold;
  color: #009879;
}
.content-table tbody td.total-row {
  font-weight: bold;
  color: #000000;
}
</style>
<p><span>Hello, </span></p>
<p><span>This is an <b>automated</b> Snapshot report which contains all VM snapshots from $VCenter VMware vCenter. Please review the attached report.</span></p>
<h3 align="left">Generated: $ReportTime</h3>
<table class="content-table">
  <thead>
    <tr>
      <th>Total number of snapshots created</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td class="total-row">$HowManySnapshots Snapshots</td>
    </tr>
  </tbody>
</table>
"@
 
    $EmailSummary = @"
<p><span>Best regards,</span></p>
<p><span>VMWare Team</span></p>
"@
  
    # Email global variables
    $SMTPServer = "SMTP_SERVER"
    $From = "noreply@youremail.com"
    $Timeout = "60"
    $Subject = "$MailTitle $((Get-Date).ToShortDateString())"
    $Message = New-Object System.Net.Mail.MailMessage
 
    $Body = $EmailBody + $EmailSummary
    $Message.Subject = $Subject
    $Message.Body = $Body
    $Message.IsBodyHtml = $true
    $Message.To.Add($Email)
    $Message.From = $From
    $Message.Attachments.Add($AttachmentFile)
    $SMTP = New-Object System.Net.Mail.SmtpClient $SMTPServer
    $SMTP.Send($Message)
}

Loading