beovax
Enthusiast
Enthusiast

Automatically remove snaphots older than X days

Jump to solution

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

Cheers

0 Kudos
1 Solution

Accepted Solutions
a2alpha
Expert
Expert

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

0 Kudos
13 Replies
a2alpha
Expert
Expert

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

0 Kudos
beovax
Enthusiast
Enthusiast

Thank you, I will give that a try

0 Kudos
a2alpha
Expert
Expert

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

0 Kudos
beovax
Enthusiast
Enthusiast

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

0 Kudos
a2alpha
Expert
Expert

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

0 Kudos
beovax
Enthusiast
Enthusiast

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?

0 Kudos
a2alpha
Expert
Expert

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

0 Kudos
beovax
Enthusiast
Enthusiast

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?

0 Kudos
a2alpha
Expert
Expert

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

0 Kudos
beovax
Enthusiast
Enthusiast

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

0 Kudos
a2alpha
Expert
Expert

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

0 Kudos
beovax
Enthusiast
Enthusiast

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

0 Kudos
a2alpha
Expert
Expert

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

0 Kudos