Finding VMware Snapshots by Quantity and Age

If you’re working in a shared vSphere environment with other Administrators, chances are you might be concerned about snapshots being left around. Regardless of the quality of a process, people are fallible and can forget to clean up after a quick “let me try this” snapshot. Unfortunately, the monitoring performed by vCenter regarding snapshots is quite poor, as you are limited to one metric: the size of the snapshot.

What about number of snapshots per VM? Or the age of a snapshot? Are these not also important? I think they are. While I’m sure there are a billion 3rd party tools out there to tackle this, I approach from a different angle.

This really doesn’t help much

Your Buddy, PowerCLI

Not that I think it’s great that we need PowerCLI in this instance. Really, this should be an available vCenter alert! But, I always like good excuses to script. 🙂

I commonly employ the same basic script and modify it as needed for an environment. The following script has been scrubbed to work in nearly any environment so that you can modify to your taste. A few lines of interest:

  • Line 2 – Make sure to change the vCenter variable to the name of your vCenter server (and add credentials if your logon account doesn’t have access).
  • Line 4 – The max snapshots a VM can have without setting off a warning
  • Line 6 – The max age (in hours) a VM can have without setting off a warning
  • Line 8 – Display all VM details? (Setting to $false lists only VMs with warnings)

[sourcecode language=”PowerShell” highlight=”2,4,6,8″]
# vCenter server name or IP
$vcenter = "vCenter5"
# Max number of snapshots to allow per VM
$maxsnapcount = 1
# Max age of any snapshot to allow in hours
$maxsnapage = 18
# Display all VM messages?
$displayall = $true

#region Code
### Static Variables

# Array containing all snapshots found
[array]$allsnapshots = $null

# Current date minus snapshot max date
$date = (Get-Date).AddHours(-1 * $maxsnapage)

### Gather Inventory
Connect-VIServer $vcenter | Out-Null

$vms = Get-VM | Get-View

Foreach ($vm in $vms)
If ($vm.Snapshot -ne $null)
# Inventory the VM snapshots
[array]$vmsnapshots = Get-VM -Id $vm.MoRef | Get-Snapshot

# Add the snapshots to the master array
$allsnapshots += $vmsnapshots

# Check to see if the VM has exceeded the max snapshot age
Foreach ($snap in $vmsnapshots)
If ($snap.Created -le $date) {Write-Host -ForegroundColor:DarkRed "[Old Snapshots] $($ contains an old snapshot named $($snap.Description) created on $($snap.Created)"}

# Check to see if the VM has exceeded the max snapshot count
If (($vmsnapshots.count) -gt $maxsnapcount)
Write-Host -ForegroundColor:DarkRed "[Max Snapshots] $($ has $($vmsnapshots.count) snapshots"
ElseIf ($displayall -eq $true) {Write-Host -ForegroundColor:DarkGreen "$($ has no snapshots"}

# Summary Information

Write-Host "==============================`nSummary Information`nFound $($vms.count) VMs`nFound $($allsnapshots.count) snapshots"


The output looks roughly like this. You can pipe it into an email if you wish, or just have it dump to your display and run on demand.