PowerCLI: Remove SMVI snapshots

I wrote this script about a year ago to deal with errant SMVI snapshots, and was drafting this blog post when my rss feed caught me off guard. It appears Matt Robinson has beat me to the punch line.   He has produced a Perl script that cleans up any leftover snapshots, but if you favor a PowerShell approach… I give you Remove-SMVISnapshots.

I can personally attest to it’s usefulness, imagine walking into this little time bomb.  It took 36 hours, but my little script that could chewed through all 370 ESX snapshots.   I now deploy this script as a standard part of any SMVI installation.  Running it as a scheduled task on the SMVI server 30 min after the nightly backups.   I haven’t tested running it as a post action in SMVI v2, but I see no reason this wouldn’t work. 

To be clear I Love SMVI it is a Critical piece of our Virtual Infrastructure. With out SMVI we wouldn’t be able to meet over half our SLA’s. I’ve talked to folks throughout the NetApp chain, and they are really working on fixing this one! Until that day, Just remember PowerShell,  Cures what Ails Ya!
~Glenn Sizemore

P.S. Again with the complexity… Perl 1500 lines / PowerShell 160 lines of very verbose code… any questions!

8 thoughts on “PowerCLI: Remove SMVI snapshots”

    • I could have, but it would take a little longer. This script could be a one liner…

      Get-VM | Get-Snapshot | ?{$_.Name -match "^smvi_"} | %{Remove-Snapshot -Confirm:$false}

      I was driven to the much more complex solution due to the size of the problems I kept running into. Dedupe was enabling 1tb NFS datastores to reach hundreds of VM’s easily(very easily). Usually not a problem, but when one of them got backed up on esx snapshots… getting out of that hole sucked. There simply are not enough IOPS to delete them all at once, so you’re forced to back them off in succession.

      These commit jobs were running so long that additional snapshot’s would be taken. That drove me to a loop to find and add these new snapshots to the delete qeue. At first something like:

      $VMs  = Get-VM | Get-Snapshot | ?{$_.Name -match "^smvi_"}
      While ($VMs.count -gt 0) {
           $VMs | select -first 3 | remove-snapshot -confim:$false -runasync | wait-task
           $VMs  = Get-VM | Get-Snapshot | ?{$_.Name -match "^smvi_"}

      The above worked but it drove me nuts watching 5 min tick away between passes waiting on get-vm | get-snapshot to enumerate a couple thousand objects…. So I went to my happy place… and get-view, as always delivered in spades.

      Carter, I know why you get on me for using the SDK, but really man you delivered the most complete automation solution I’ve EVER seen, and you did it in one cmdlet. You guys should be proud of Get-View. I don’t always use it… truth be told I never use it at the console. Only when scripting, but I know it’s there when/if I ever need it.

      It’s no more complicated than WMI/ADSI… I appreciate the layers of abstraction, but really we can handle the SDK. All kidding aside I will reassess. The performance in U1 is So much better!!! I would be willing to bet I could go back to the while loop.


      P.S. Yes I have PAM, and yes that’s why I can run such large datastores. I still love NetApp, this isn’t a fault of NetApp or VMware. It a victim of circumstance, and one that is easily worked around. (Thanks to PowerCLI!)

  1. Great script, I’m definitely giving this a try, thanks! Although not as a post script, considering SMVI 2.0 has a confirmed bug running scripts that makes certain scheduled jobs crash. I really hope the next version of SMVI is a) documented properly and b) doesn’t contain such obvious bugs.

  2. Glenn I’m not getting on you, I’m a big fan of whatever works. But 99 guys out of 100 couldn’t have come up with what you did so if we need to be better I just want to understand how.

    One thing we’ve been thinking about is a universal parameter to limit the number of jobs that are kicked off at once. Something along the lines of “Remove-Snapshot -MaxJobs 3” Do you think you could have avoided the complexity if you had that?

    • With U1’s performance… Yeah, I think a maxjobs parameter would drastically decrease the complexity! I tested some over the weekend, and while my get-snapinfo function is still faster. Get-vm|Get-Snapshot is fast enough. Add a throttling mechanism, and I could easily be back down to a one liner.

      Get-VM|Get-Snapshot|Remove-Snapshot -MaxJobs 3 -Verbose -Confim:$false

      I like it!

  3. im running this script and i get this error and then it spams VC with remove snap shot but for the same 3 VMs.

    Wait-Task : 12/22/2011 10:00:17 AM Wait-Task Value cannot be null.
    Parameter name: key
    At C:ScriptsRemove-SMVISnapshots.ps1:138 char:18
    + Wait-Task <<<< -Task $tasks -Verbose:$false
    + CategoryInfo : NotSpecified: (:) [Wait-Task], VimException
    + FullyQualifiedErrorId : Core_BaseCmdlet_UnknownError,VMware.VimAutomation.ViCore.Cmdlets.Com

  4. What am I missing here? SMVI takes a vm snapshot, then a netapsnapshot, then deletes the vm snapshot. So get-snapshot doesnt return anything, only get-nasnapshot will….

    • Hey John,

      Older versions of SMVI would sometimes fail to delete the vSphere snapshot. Once it failed it would fail on every consecutive job, and you would quickly get into a very bad situation with multiple nested snapshots.

      Recent versions of VCS take care of this problem rendering this script obsolete.



Leave a Reply