VMWare Snapshots Report

VMWare Snapshots Report

A quick powershell script using HTML and CSS to run against your VMware Environment utilising PowerCLI to pull back a report of your snapshots.

This can be useful for reclaiming space and making the most of your storage or to spot somewhere that you have a usage issue.

This does make the assumption you are using the same credentials to log into each of your VCentres, if you are using differing ones, a service account with the correct permissions on each could be created to run this report.

#Import the PowerCLI module
Import-Module VMware.PowerCLI

# Record the start time for measuring execution time
$startTime = Get-Date

# Get valid credentials
$creds = Get-Credential
$vcsausername = $creds.UserName

# Create an array to store VM snapshot data
$vmData = @()

# List of vCenter servers (modify as needed)
$vCenterServers = @("vcsa1.your.domain.com", "vcsa2.your.domain.com")

foreach ($vCenterServer in $vCenterServers) {
# Connect to vCenter server
Connect-VIServer -Server $vCenterServer -Credential $creds

# Retrieve VMs with snapshots
$vmsWithSnapshots = Get-VM | Where-Object { $_ | Get-Snapshot -ErrorAction SilentlyContinue }

# Iterate through VMs with snapshots
foreach ($vm in $vmsWithSnapshots) {
$snapshots = $vm | Get-Snapshot

foreach ($snapshot in $snapshots) {
# Calculate snapshot age in days
$snapshotAge = (Get-Date) – $snapshot.Created

$vmData += [PSCustomObject]@{
VDC = $vm.VMHost.Parent.Name
VM = $vm.Name
SnapshotName = $snapshot.Name
SnapshotDescription = $snapshot.Description
SnapshotSize = "{0:N2} GB" -f $snapshot.SizeGB
SnapshotCreated = $snapshot.Created
SnapshotAgeInDays = [math]::Round($snapshotAge.TotalDays, 2)

# Disconnect from vCenter server
Disconnect-VIServer -Server $vCenterServer -Confirm:$false

# Sort the results by SnapshotAgeInDays from largest to smallest
$vmData = $vmData | Sort-Object -Property SnapshotAgeInDays -Descending

# Custom CSS for the table and dark mode
$customCSS = @"
<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-wp-preserve="%3Cstyle%3E%0A%20%20%20%20body%20%7B%0A%20%20%20%20%20%20%20%20background-color%3A%20%23333%3B%0A%20%20%20%20%20%20%20%20color%3A%20%23fff%3B%0A%20%20%20%20%7D%0A%20%20%20%20table%20%7B%0A%20%20%20%20%20%20%20%20width%3A%20100%25%3B%0A%20%20%20%20%20%20%20%20border-collapse%3A%20collapse%3B%0A%20%20%20%20%20%20%20%20font-size%3A%208pt%3B%20%2F*%20Set%20font%20size%20to%208%20points%20*%2F%0A%20%20%20%20%20%20%20%20background-color%3A%20%23444%3B%0A%20%20%20%20%7D%0A%20%20%20%20th%2C%20td%20%7B%0A%20%20%20%20%20%20%20%20border%3A%201px%20solid%20%23ddd%3B%0A%20%20%20%20%20%20%20%20padding%3A%208px%3B%0A%20%20%20%20%20%20%20%20text-align%3A%20left%3B%0A%20%20%20%20%7D%0A%20%20%20%20th%20%7B%0A%20%20%20%20%20%20%20%20background-color%3A%20%23666%3B%0A%20%20%20%20%7D%0A%20%20%20%20p%20%7B%0A%20%20%20%20%20%20%20%20font-size%3A%208pt%3B%20%2F*%20Set%20font%20size%20to%208%20points%20*%2F%0A%20%20%20%20%7D%0A%3C%2Fstyle%3E" data-mce-resize="false" data-mce-placeholder="1" class="mce-object" width="20" height="20" alt="&lt;style&gt;" title="&lt;style&gt;" />

# Create an HTML report with custom CSS
$logoURL = "https://www.logostock.com/logo.png"
$htmlReport = @"
<!DOCTYPE html>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet">
<div class="container">
<img src="$logoURL" alt="Logo" style="max-width: 200px;">
<h1>Snapshot Report</h1>
$($vmData | ConvertTo-HTML -Fragment)
<br> <!– Add a line break –>
<p>Report ran on $((Get-Date).ToString(‘dd MMMM yyyy’)) by $($env:USERNAME) using $($vcsausername) to authenticate to VCSA. <br>
Report run time was $((Get-Date) – $startTime).</p>

# Output the HTML report to a file
$htmlReport | Out-File -FilePath "c:\temp\SnapshotReport.html"

# Display the HTML report
Invoke-Item "c:\temp\SnapshotReport.html"



No comments yet. Why don’t you start the discussion?

Leave a Reply

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