Bas Vinken over at the Kadaster (the Dutch Land Registry Office) has written a PowerShell script with great potential. It first detects and then removes the snapshots from all your virtual machines. This script uses the PowerShell snap-in from the VI-Toolkit. Great work Bas.
Get-VIServer "VCSERVER" -User bas -Password vinken
$AllVirtualMachines = Get-VM
foreach ($VirtualMachine in $AllVirtualMachines)
{$AllSnapshots=Get-Snapshot -VM $VirtualMachine
foreach ($Snapshot in $AllSnapshots)
{If ($Snapshot.ID -like "VirtualMachineSnapshot-*")
{Write-Host $VirtualMachine.Name, $Snapshot.Name, $Snapshot.Description
Remove-Snapshot -snapshot $Snapshot –Confirm Write-Host } } }









You don't need a script to do the above--a very, VERY simple one-liner will do. The script does not show the power of PowerShell at all! Once you have authenticated with Get-ViServer, to remove all snapshots on all your VMs, it's as easy as this:
Get-Vm | Get-Snapshot | Remove-Snapshot
Or to do a subset, you can add a string after Get-Vm for a wildcard match on the VM names. Don't forget the pipeline, Bas and Eric!
ttyl
Hal, co-host of PowerScripting Podcast (http://powerscripting.net), and VI/PS blogger at TechProsaic (http://halr9000.com).
I understand that the script could be a one-liner, but then it's all or nothing.
I do not know in advance where all the snapshots are and if I want to remove them.
What I need is a choice based on as much info as possible.
That's why I use the line Write-Host $VirtualMachine.Name, $Snapshot.Name, $Snapshot.Description.
You can only convince me by showing me a one-liner which produces the same
output as the script above.
$a = get-vm patch* | foreach-object { $snap = get-snapshot $; write-host $.name "`t" $snap.Name "`t" $snap.description ;
remove-snapshot $snap -confirm }
And the output:
patchvistaent_Vista Enterprise 2007-11-29 Pre-Dec drop test
Confirm
Are you sure you want to perform this action?
Performing operation "Removing snapshot" on Target
"VirtualMachineSnapshot-snapshot-2048".
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"):