Yet another PowerCLI script for VM snapshot reporting.

OK, after reading the title of this post you might have thought something like: “Well, this guy must have been born yesterday” or “What now? Are we really reinventing the wheel here?”.
And yeah, you’re probably right in the way that reporting on VMware snapshots in your infrastructure is one of the best documented topics out there and probably subject of the biggest number of PowerCLI scripts on the Web.

I mean – everybody has written a script for that, right?

If you nevertheless choose to continue reading, you’d probably notice, that sending automated e-mail notifications to “owners” of “old” snapshots is generally inspired by Alan Renouf’s classic “snapreminder” script.
Well, can’t deny that either, to my defense I can only tell, I’m taking examples from best of the best 😉 .

There is one feature of this script that makes it “special” to me however. Namely, this is one of my first PowerCLI scripts where I (successfuly!) use get-view cmdlet and leverage the power of .Net views offered via PowerCLI.

So let’s have a look:

The display of my best PowerCLI kung-fu so far takes place in Line 167, where I’m retrieving a .Net view of VirtualMachine type and at the same time I filter-out only the VMs that have “anything” in the “snapshot” view property. Then I pipe it through a tiny script block where “standard” get-vm and get-snapshot PowerCLI cmdlets retrieve full-detail information about these VMs, that I will process further to create a  CSV report about snapshots.

Why use get-view in this “hybrid” way?

Well, I was (again) inspired by excellent post of my colleague Grzegorz Kulikowski.
Being a total beginner with views I wanted to take advantage of the great speed they offered when filtering-out information initially, but I wasn’t feeling comfortable enough to continue using views and get all the required details myself, so I leveraged all the power of “thick”, or “feature-rich” PowerCLI cmdlets.

And it worked for me!

Quick measurements showed that “standard” sequence used to find VMware snapshots (get-vm | get-snapshot) never takes less than 12 seconds (in a rather small environment of ~1000 VMs).
Compared to that, the sequence from Line 167 is almost 10 times faster! (around 1.5 seconds in the same environment).
This is because with “standard” sequence first get-vm collects information about all VMs in our environment, then fills these VM objects with many properties, that we simply don’t need for our report.
Only the second cmdlet (get-snapshot) provides us with snapshot objects that we really care about.
When using get-view we immediately receive (references to) only the VMs that have active snapshots and “thick” PowerCLI cmdlets are applied to this (limited subset of) VMs only.

To be honest not much more exciting things are happening in this script.
As I stated at the beginning, reporting on snapshots is PowerCLI 101, right?

Between Line 169 and Line 182 a helper object that will be used as a single “row” in the report is created for each snapshot found.

Between Line 185 and Line 195 script searches the vCenter event database to find the snapshot’s creator username.
Please note, there is no point to search for “snapshot creation” events if the snapshot is older than event database retention period, i.e. $event_age.Value that we retrieved in Line 164.

Creator’s username is stored in “domain\login” format, so I define Find-User function that will search your Active Directory to find “displayname” and “mail” properties for this account.
The script uses them to send automated e-mail notifications, when snapshot age exceeds $notify_threshold that I set to 14 days.
The assumption here is that “displayname” in your AD has the format of “firstname-single-space-surname”.
If it looks anyhow different (for example “surname, firstname”) you need to add some string manipulation commands to Find-User function (if you still want to have the e-mail message to snapshot owner to be nicely composed).

All the data manipulation that happens between Line 223 and Line 237 is there just to compose an e-mail message to each detected owner of outdated snapshot.
I decided to create one e-mail containing notification about all outdated snapshots per user, rather than “spam” users with many messages (if they tend to leave many snapshots behind them).

That’s more or less it – I hope you find this post useful, feel free to share it and/or provide your feedback.

Sebastian Baryło

Successfully jumping to conclusions since 2001.

  • Pingback: Tools for monitoring and deleting VMware snapshots | Settlersoman()

  • yogendra singh

    good one, but if the snapshot size is below 1 gb it shows as 0.00, i made it as mb and working fine.

  • Graham

    Any way to show that if a Snapshot has not been taken ?

    I like this script, its great to see all VMs that has a snapshot in place but I would also like the output to show what VMs does NOT have a snapshot taken. Is this possible ?

    Thanks

  • Bradley Sessions

    Any advice on adding the Snapshot Name, Size, and Create Time to the email that is sent out?