Managing Snapshots with PowerCLI

With access to vCenter or vRealize Automation comes great responsibility. Clients often require a quick and easy way to take a short term snapshot of a server so that they can quick and easily rollback application changes. VMware provides a robust snapshot function, however when things go well these snapshots are often left behind or forgotten about.

Run PowerCLI and run the below commands to get a list of all VMs with a Snapshot

1Get-VM | Get-Snapshot

To make the output a little nicer on the eye, run the following command

1Get-VM | Get-Snapshot | Select VM,Name,Description,{[int]$_.SizeMB},Created

To limit the results to only snapshots that are greater than 7 days old, run the following command

1Get-VM | Get-Snapshot | Where { $_.Created -lt (Get-Date).AddDays(-7)} | Select VM,Name,Description,{[int]$_.SizeMB},Created

To remove all snapshots from a server, run the below command. Include the -RunAsync command to run the snapshot removal as a background task instead of waiting for the removal to complete.

1Get-VM -Name "x" | Get-Snapshot | Remove-Snapshot -RunAsync

I have also written a basic PowerShell tool to manage Snapshots. If you encounter any issues with it, just let me know via the Contact page and I will happily provide assistance.

SnapshotManagement.ps1

comments powered by Disqus