Get VM snapshots older than a particular number of days

I recently wanted to return a list of snapshots which were older than 3 days within my environment, this was primarily to return any snapshots that had been created by third party applications that take advantage of snapshots. This would prevent no single snapshot from growing to be large in size, which may impact on VM performance.

I compiled a powershell script leveraging vSphere PowerCLI to capture the snapshots that were older than a particular number of days, in my case and for the below example I will be using 3 days as mentioned above.

Firstly, I would need to register the PowerCLI snap-ins to the current powershell session and then connect to the vCenter server (in this example, the vCenter server is named SERVER1);

# Adds registered vSphere PowerCLI snap-ins to the current session
Add-PSsnapin VMware.VimAutomation.Core >$null

# Connect to the vCenter Server
Connect-VIServer SERVER1 >$null # Enter the name of your vCenter server

Once I have established a connection to the vCenter server, I will retrieve all virtual machines and then pipe the results to the Get-Snapshot cmdlet. As I do not require to return all snapshots that have been created only those that are older than 3 days, I will be required to filter the results based on the Created attribute of the snapshot. This is achieved by using the subtract method to the DateTime object.

Finally, I wanted to only return a number of attributes such as the VM name (VM), Snapshot Name (Name), Snapshot Created Timestamp (Created) and Snapshot size (SizeMB) so these objects will be selected for the output

# Retrieve VM snapshots older than 3 days
Get-VM | Get-Snapshot | Where {$_.Created -lt (Get-Date).AddDays(-3)} | Select-Object VM, Name, Created, SizeMB

Alternatively, it is possible to create alarms within vCenter to alert when a virtual machine snapshot is a certain size; this following article details these steps:

http://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=1018029

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s