PowerCLI Script: Snapshots Report

Keeping a VM snapshot for too long is not a good idea. The VM snapshot file grows over time and you may eventually run out of storage space.

If you have a large infrastructure, hundreds or even thousands of virtual machines, managing snapshots may not be easy. The snapshot report script is very useful here.

The script presented in this article can be found at My GitHub Repository


Prerequisites

Powershell

Make sure you have the latest version of Powershell. Go to this website and download/upgrade your Powershell.

PowerCLI module

Use the following command to install PowerCLI module in your Powershell.

Install-Module -Name VMware.PowerCLI

Once done, run the following command. This is to avoid potential issues with SSL connection to vCenter.

Set-PowerCLIConfiguration -InvalidCertificateAction Ignore

Script

Modify the script below to add your vCenter IP/Hostname.

You can also modify the location of the output file, the default is C:\temp

Run the script, you will be asked for your vCenter credentials.

# vmcloud.pl
# Connect to vCenter Server
Connect-VIServer <vCenter_IP>

# Get all VMs
$VMs = Get-VM

# Create empty array to hold snapshot information
$SnapshotInfo = @()

# Loop through each VM and get snapshot information
foreach ($VM in $VMs) {
    $Snapshots = Get-Snapshot -VM $VM

    # Loop through each snapshot and add information to array
    foreach ($Snapshot in $Snapshots) {
        $SnapshotInfo += [PSCustomObject]@{
            VMName = $VM.Name
            SnapshotName = $Snapshot.Name
            Created = $Snapshot.Created
            Description = $Snapshot.Description
            SizeGB = [math]::Round(($Snapshot.SizeGB),2)
        }
    }
}

# Create HTML output
$HTML = @"
<html>
<head>
<title>Snapshots Report</title>
<style>
    body {background-color: #D3D3D3;}
    table {border: 3px solid black; border-collapse: collapse; font-size: 14px; font-family: Arial, sans-serif; background-color: #3B3B3B; color: white; max-width: 800px;}
    th, td {border: 2px solid black; padding: 10px; text-align: left; width: auto;}
    th {background-color: #2F4F4F; font-weight: bold;}
</style>
</head>
<body>
"@
$HTML += $SnapshotInfo | ConvertTo-Html -Fragment -Property VMName, SnapshotName, Created, Description, SizeGB
$HTML += @"
</body>
</html>
"@

# Output HTML to file
$FilePath = "C:\temp\SnapshotInfo.html"
$HTML | Out-File -FilePath $FilePath

# Output confirmation message
Write-Host "Generating Snapshots Report"
Write-Host "Your snapshots report has been saved to: $FilePath"

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

Results

Running the script:

Result:

If you prefer the output file in Excel format, modify the script, or simply copy the generated table into Excel.


Thank you for reading! Here are some links you may be interested in:

One thought on “PowerCLI Script: Snapshots Report

Add yours

  1. Nice script 🙂 How to add more column in the report to get the datastore name of each VM with snapshot, datastore size, datastore free size, and User who created the snapshot

    Like

Leave a comment

Create a website or blog at WordPress.com

Up ↑