Close Menu
PowerShell.roPowerShell.ro
  • Home
  • Automation
  • Powershell
  • VMWare
  • Windows
LinkedIn X (Twitter) Facebook
PowerShell.roPowerShell.ro
  • Home
  • Automation
  • Powershell
  • VMWare
  • Windows
LinkedIn Facebook X (Twitter)
PowerShell.roPowerShell.ro
Home»Powershell»Generate Detailed Snapshots Report using PowerCLI Scripting
Powershell

Generate Detailed Snapshots Report using PowerCLI Scripting

Catalin CristescuBy Catalin CristescuJune 15, 2023
Share Facebook Twitter Pinterest LinkedIn Tumblr Reddit Telegram Email
Share
Facebook Twitter LinkedIn Pinterest Email

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)
}

powercli powershell scripting snapshot report vcenter vmware
Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
Next Article PowerCLI – increase C drive on Windows server
Catalin Cristescu
  • Website

Related Posts

Automation

PowerShell script for automated remediation for CrowdStrike issue

July 20, 2024
VMWare

vExperts stats 2024

May 27, 2024
Automation

Tagging Virtual Machines with the CISTag Module in PowerShell

February 1, 2024

Comments are closed.

Recent Posts
  • PowerShell script for automated remediation for CrowdStrike issue
  • vExperts stats 2024
  • Tagging Virtual Machines with the CISTag Module in PowerShell
  • Update HPE ILO firmware using PowerShell
  • PowerCLI script to check Cluster Overcommitment ratio
Categories
  • Automation (11)
  • Powershell (14)
  • Uncategorized (1)
  • VMWare (15)
  • Windows (2)
About Powershell.ro

Powershell.ro, the ultimate hub for tech enthusiasts! Dive into expert-written articles on PowerShell scripts, VMware, automation, Microsoft technologies, and DevOps practices. Unlock tips, tutorials, and solutions designed to empower IT professionals and boost your skills in automation and infrastructure management.

Recent Posts
  • PowerShell script for automated remediation for CrowdStrike issue
  • vExperts stats 2024
  • Tagging Virtual Machines with the CISTag Module in PowerShell
  • Update HPE ILO firmware using PowerShell
  • PowerCLI script to check Cluster Overcommitment ratio
LinkedIn
© 2025 PowerShell.ro.

Type above and press Enter to search. Press Esc to cancel.