VMware Cloud Community
beovax
Enthusiast
Enthusiast
Jump to solution

Automatically remove snaphots older than X days

Does any one know of a way to automatically commit (delete) shapshots older than X number days?

Cheers

Reply
0 Kudos
1 Solution

Accepted Solutions
a2alpha
Expert
Expert
Jump to solution

I would start here, this script lists the snapshot and then asks for confirmation for removal, it has the created time so you could use that to automate the removal, in the meantime this is a quick way of removing the snapshots from your environment in a partially automated way.

Get-VIServer "VISERVER" -user admin -password pa$$w0rd

echo ""

echo "Snapshot Remover"

echo ""

$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, $Snapshot.Created

Remove-Snapshot -snapshot $Snapshot -Confirm } } }

Hope this helps,

Dan

View solution in original post

Reply
0 Kudos
13 Replies
a2alpha
Expert
Expert
Jump to solution

I would start here, this script lists the snapshot and then asks for confirmation for removal, it has the created time so you could use that to automate the removal, in the meantime this is a quick way of removing the snapshots from your environment in a partially automated way.

Get-VIServer "VISERVER" -user admin -password pa$$w0rd

echo ""

echo "Snapshot Remover"

echo ""

$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, $Snapshot.Created

Remove-Snapshot -snapshot $Snapshot -Confirm } } }

Hope this helps,

Dan

Reply
0 Kudos
beovax
Enthusiast
Enthusiast
Jump to solution

Thank you, I will give that a try

Reply
0 Kudos
a2alpha
Expert
Expert
Jump to solution

Decided to take up the challenge!!

Try this, I have kept in the confirmation part so if the snapshot is older than x days it will ask for confirmation for deletion. This is really just there whilst you test it so it doesn't go off and commit all snapshots when you stuck 1 instead of 10 days.

You can remove the -confirm option once you are happy and take out the $args[0] section and replace with the acutal number of days you want (if it will always be the same) say 30 days.

So to start with run it from Powershell with ./snapremdate.ps1 30

Which will check for snapshots older than 30 days before today and then ask for confirmation to commit.

Hope this is what you were looking for

Dan

(the script got messed up so use the attached file!!)

Message was edited by: a2alpha

Reply
0 Kudos
beovax
Enthusiast
Enthusiast
Jump to solution

Now thats community spirit! I will test it and let you know - PM me your email address and ill send you donation via paypal Smiley Happy

Reply
0 Kudos
a2alpha
Expert
Expert
Jump to solution

Sorry, also mean't to say that you could run this as a scheduled task from a machine with the vi-toolkit and powershell installed and stick you 30 days or whatever in the command section. Let me know how it goes!

Dan

Reply
0 Kudos
beovax
Enthusiast
Enthusiast
Jump to solution

First time i have ever used the powershell, how does the system loginto VC? I can see the line for the virtual server name, does it just use the creditials that started the script?

Reply
0 Kudos
a2alpha
Expert
Expert
Jump to solution

yep, it will use your windows session credentials, if that doesn't work, a windows login box appears and will ask for credentials then.

You could change the line to:

connect-viserver vCenter -user michael -password Pa$$w0rd if you wanted to run it through automatically.

Dan

Reply
0 Kudos
beovax
Enthusiast
Enthusiast
Jump to solution

Works like a dream!! just need to check the day counter is working all OK, but im sure it is. When the script starts it flashes up with the attached error - looks liek it is trying to start something which is already running?

Reply
0 Kudos
a2alpha
Expert
Expert
Jump to solution

If you run it from the VI-Toolkit powershell then you get that error (because that is just normal powershell with the vi pssnapin added).

If you will always run it from there, remove the add pssnapin line, if you call it from normal powershell (say from scheduled task) you would need it so that powershell can interpret you vmware commands, like get-snapshot etc.

I'm really happy it works, its stuff like this that really gets you into powershell when it will save you time and effort!!

Dan

Reply
0 Kudos
beovax
Enthusiast
Enthusiast
Jump to solution

Ahh I see -

Still get one other error when running directly from the command line (sorry if I still havent got this right) doesnt seem to like initialize-VItoolkitenvironment.ps1

I'm uisng vSphere with V4 toolkit, maybe this the problem?

Still all works OK though, more cosmetic than anything

Reply
0 Kudos
a2alpha
Expert
Expert
Jump to solution

Try running:

set-executionpolicy unrestricted

will stop you having to press r to run it each time.

As for the Initialize error, try referencing its location (and make sure its there) by putting this in the script

Add-PSSnapin VMware.VimAutomation.Core

. "C:\Program Files\VMware\Infrastructure\VIToolkitForWindows\Scripts\Initialize-VIToolkitEnvironment.ps1"

Dan

Reply
0 Kudos
beovax
Enthusiast
Enthusiast
Jump to solution

ill give that a try - I have already set the execution policy to unrestricted, still promtps me...

May not be able to try the change to later today, but I will let you know - thanks for your help

Reply
0 Kudos
a2alpha
Expert
Expert
Jump to solution

No problem,

Just realised, right-click on the snapremdate.ps1 file select properties and then select unblock.

This should get rid of the request for running it.

Dan

Reply
0 Kudos