On one of my previous articles Sven, who is actually a colleque of mine, asked if it would be possible to remove snapshots which were left unaccounted for. I reacted is a simple way: yeah, of course and as a matter of fact is is very easy. I really must say the guys from the VI Toolkit have done a great job on making a lot of complicated things very easy !! Hats off guys.
When I was doing a little background check for this I found another script made by Bas Vinken which can be found here, but what I like about Powershell scripting are what are known to be “Oneliners” all code on a single line. This is what shows the real power of powershell and that’s what I want to show everybody.
So when I created this script I had one thing in mind: “Keep it simple !”. Ok here the script:
$snap=get-snapshot -vm * ; remove-snapshot -snapshot $snap
What does it do ?
The Get-snapshot -vm * queries the connected VI server for all VM’s with associated snapshots, the $snap= statement puts these values in the variable $snap, the remove-snapshot commandlet uses this variable to …… commit the snapshot to the Virtual Disk. The “;” between the 2 commandlets is nothing else than a seperator instead of a hardreturn (otherwise it wouldn’t be a oneliner .. duh)
I cannot emphasize the importance of carefullness of using these types of scripts, whatif a snapshot has been made of a disk that has real userdata or an exchange database on it and you revert to a pre-snapshot state ? That’s why I really hope that everybody uses persistent disks for these kind of data containers. But fortunatly there is not an commandlet to revert to a previous snapshot.