VMware Powershell script - Remove snapshots
by Johan van Zanten VMware Powershell scripts Add commentsOn 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.

July 30th, 2008 at 1:42 pm
Nice work Johan!
Especially the fact that it’s a one-liner
[Reply]
August 14th, 2008 at 1:16 am
Is there anyway to remove a VM completely via Powershell/Toolkit?
[Reply]
Sven Huisman reply on August 14th, 2008 9:07 am:
This is from the help-file:
C:\PS>Remove-VM ( Get-VM myVM ) -DeleteFromDisk
Removes the MyVM virtual machine and deletes its files from the ESX server.
[Reply]
August 19th, 2008 at 3:46 pm
How could you define a snapshot name? Say the _VCB-BACKUP_ snapshots. Would it be a bad idea to delete the VCB snapshots automagicly?
[Reply]
Johan van Zanten reply on August 21st, 2008 8:13 pm:
Hi Mike,
You’ve got 2 questions, the answer to the first one:
Actually that’s really easy to do; you have to make an selection with the where statement. I updated the powershell script I created previously:
$snap= Get-Snapshot -vm *|where {$_.Name -match “VCB-Backup”} ; remove-snapshot -snapshot $snap
You see it’s really easy..
The answer to the second question: snapshots have an expiry date; let me explain: when you create a snapshot the domain password has a certain value, this value will change regularly so when you revert to a snapshot after the domain password has changed you need to remove the machine from the domain and add it again.
That opens an discussion I will go into in a new article, the use and unuse of snapshots.
I hope this answered your questions..
grtx Johan
[Reply]
August 22nd, 2008 at 8:16 pm
Well the good news is I think the first part of the response was what I am looking for, need to test. The second part… Im not sure where you were going with that one. I must have been unclear but good info anyways
Thank you sir!
[Reply]
Johan van Zanten reply on August 28th, 2008 4:00 pm:
Hi Mike,
You’re welcome
Let me put in other words: when you snapshot a machine and a user changes his data on a share, after some time you revert the snapshot, how do you think that user reacts when he finds out that his changed data has been reverted as well ?
just my 2 cents
grtx Johan
[Reply]
Hal Rottenberg reply on August 28th, 2008 4:09 pm:
Johan,
In the case of a file server, best practices indicate you would only snapshot the OS drive. You would then store files and home directories on a second hard drive and that drive would be marked independent so that its it not affected by snapshots. Of course you would still need to perform conventional backups of these files.
[Reply]
Johan van Zanten reply on August 28th, 2008 4:25 pm:
Hi Hal,
Once again, you’re absolutely right, never snapshot user data. As a rule of thumb I only snapshot the systemdrive and make the data drives persistent ones.
But what are the defaults and how many admins would or just stick to defaults ?
My opinion on using snapshots keep them only as long as you need them to test the system (e.g. in case of servicepack or upgrade)
grtx Johan
August 25th, 2008 at 4:16 am
I’m sorry I missed your blog before now, you’ve got some good stuff here (and a nice theme). Some comments:
1. The script can be made even shorter if that can be imagined, and this way feels a lot more logical to me. Once you use the pipeline, you will never go back…
get-vm | get-snapshot | remove-snapshot
And by the way–there is a cmdlet to revert to a previous snapshot. Check out Set-VM’s Snapshot parameter.
-hal
Co-Host, PowerScripting Podcast (powerscripting.net) and author of the upcoming Managing VI with PowerShell book.
[Reply]
Johan van Zanten reply on August 28th, 2008 4:18 pm:
Hi Hal,
You’re absolutely right. I see what you mean: I rewrote the other oneliner:
get-vm | get-snapshot|Where {$_.Name -match “_VCB-Backup_”}|remove-snapshot
Without the use of the variable it’s easier to read and the pipe handles it all..
thanx we all learn from each other
grtx Johan
[Reply]
August 28th, 2008 at 4:27 pm
Thanks all
[Reply]
August 28th, 2008 at 5:02 pm
Johen,
You are right…the default setting is for a drive to be “dependent” I guess is the right way to say that.
And yeah, I once had a problem back when I was new to VMware. I had about 20 VMs which I snapshot monthly as a part of a routine. After a few months I was experiencing some AWFUL perf issues. Turned out it was due to too many snapshots with too much churn. Snapshot removal took many, many hours.
[Reply]
October 8th, 2008 at 11:42 pm
Hello All,
Thanks for your help in the past. After trying the script I have found that it runs and does what it should but I have a question.
First, imagine an environment that had 50-100 ESX hosts with hundreds of VMs. One might find several to a dozen VM’s daily with snapshots left out there.
How could I run the script; get-vm | get-snapshot|Where {$_.Name -match “_VCB-Backup_”}|remove-snapshot where confirmation wasn’t needed for each? I have added the -confirm$false but it resulted in timeout errors and looked like it might have overburdened a ESX host. The host appeared offline until I killed the script.
The following is the error that resulted.
PS C:\Program Files\VMware\Infrastructure\VIToolkitForWindows> get-vm | get-snap
shot|Where {$_.Name -match “_VCB-Backup_”}|remove-snapshot -confirm:$false
Remove-Snapshot : 10/8/2008 5:13:06 PM Remove-Snapshot D4B5D67A-77FD-4EE0
-A842-2A4E3021802D The operation for the entity vm-13948 failed with the fol
lowing message: “Operation timed out.”
At line:1 char:76
+ get-vm | get-snapshot|Where {$_.Name -match “_VCB-Backup_”}|remove-snapshot
<<<< -confirm:$false
Remove-Snapshot : 10/8/2008 5:25:13 PM Remove-Snapshot D4B5D67A-77FD-4EE0
-A842-2A4E3021802D The request refers to an object that no longer exists or
has never existed.
At line:1 char:76
+ get-vm | get-snapshot|Where {$_.Name -match “_VCB-Backup_”}|remove-snapshot
<<<< -confirm:$false
Remove-Snapshot : 10/8/2008 5:25:13 PM Remove-Snapshot D4B5D67A-77FD-4EE0
-A842-2A4E3021802D The request refers to an object that no longer exists or
has never existed.
At line:1 char:76
+ get-vm | get-snapshot|Where {$_.Name -match “_VCB-Backup_”}|remove-snapshot
<<<< -confirm:$false
Remove-Snapshot : 10/8/2008 5:25:13 PM Remove-Snapshot D4B5D67A-77FD-4EE0
-A842-2A4E3021802D The request refers to an object that no longer exists or
has never existed.
At line:1 char:76
+ get-vm | get-snapshot|Where {$_.Name -match “_VCB-Backup_”}|remove-snapshot
<<<< -confirm:$false
Remove-Snapshot : 10/8/2008 5:25:14 PM Remove-Snapshot D4B5D67A-77FD-4EE0
-A842-2A4E3021802D The request refers to an object that no longer exists or
has never existed.
At line:1 char:76
+ get-vm | get-snapshot|Where {$_.Name -match “_VCB-Backup_”}|remove-snapshot
<<<
I would like to automate this clean up. As long as that _VCB-Backup_ is the qualifier I am ok with it running automaticly.
Thanks again,
Mike
[Reply]
October 8th, 2008 at 11:49 pm
The snapshots did appear to clean themselves up but the effects are definately unacceptable.
[Reply]
October 28th, 2008 at 7:39 pm
Mike- I guess nobody has an answer for you!
[Reply]
October 31st, 2008 at 2:06 pm
Hi Mike,
Sorry that I haven’t seen your comment sooner, you could have mailed me for speeding up things. One thing I noticed with the commandline is that you have a : between -confirm and $false that should not be the case ..
grtx Johan
[Reply]