PowerCLI: Speed boost, Find VM Snapshots by Name.

I use NetApp SnapManager for Virtual Infrastructure(SMVI) to back up 95% of my environment. SMVI isn’t perfect, and it’s kinda a pain, but it gives me the ability to back up 100-250 vm’s in less than 10min! Well the actual NetApp snapshot takes seconds. The rest of the time is spent waiting on ESX snapshots. The only real downside here is from time to time SMVI will fail to delete the ESX snapshots. I have been using PowerCLI to find and delete these snapshot, but the cmdlets are just too slow, for what I’m trying to do. I’ll post my Finalized SMVI cleanup script later. Until then, I give you finding snapshots really fast!

To start with the traditional PowerCLI cmdline. Simple, powerful, and intuitive, but all those things at a price. All Times based on a small farm 8 ESX hosts 100+ VM’s.

Example: Execution Time(108 Seconds)

That’s just not fast enough for me. I’m looking for a specific snapshot in every VM. To scale this I knew I needed to dig in to the SDK. I decided to break the problem down into two steps.
1. Find any vm with a snapshot.
2. Filter the snapshots on those vm’s by Name.

Step one turned out to be real easy, and scary fast!

Example: Execution Time(0.103 Seconds)

You’re eyes aren’t deceiving you that’s a 103 milliseconds! Don’t get too excited though because we still have to filter this data. The Get-Snapshot CmdLet did all this work for us before. By leveraging Get-View I gained performance, at the cost of increased complexity… The real performance boost comes from only tasking the property collector with a single property to return. to put this in perspective Get-VM returns hundreds by default.

Step two; have I mentioned how much I love Advanced Functions!

There really isn’t anything fancy In that function… well except for the fact that it’s fast as all get out!

Example: Execution Time(0.161 Seconds)

Example: Execution Time(0.187 Seconds)

the result:
Note: the Output shown above, reports a total execution time of 332ms the additional 100ms can be tracked back to PowerShell actually writing the output to the host.

There you have it, I can now find any snapshot by name in milliseconds, how cool is that! On a side note; I hope I’ve driven the point home that PowerCLI is actually quite fast given the amount of work it does. When I develop these one off solutions I increase the performance only because I limit the scope of work! Something to keep in mind if you’re trying to speed up a script, the SDK isn’t any faster, it just allows you to be more targeted.


6 thoughts on “PowerCLI: Speed boost, Find VM Snapshots by Name.”

  1. U1 will be much faster. Still, Get-VM | Get-Snapshot will continue to load a lot more data than what Glenn is doing with his optimized function, so that will continue to be much faster. In the future future (not this year) we’re looking at the possibility of adding or removing properties from output objects, so you could get a more targeted set of data much faster.

    • Carter,
      I can’t decide if you guys should worry about this or not. On the one hand I think as long as you perform in the 10 – 150 VM environment you guys are fine. I don’t think I’ll always have to write these little one-offs for the 1000+ environments. On the other hand… it is VMware and the 1000+ will only get more common. So perhaps 1000 is the new 10-100. The ability to limit the property collector would go a long way I think.

      @LucD yeah, the fine line will always be when “should” you use get-view, what’s the threshold?

  2. HI, I am helping out a vcenter admin with powershell and getting snapshot info. How do you correlate the vm identifier output in your script to the actual host name?


Leave a Reply