Hi All,
I have a need to take a daily snapshot of a VM. I need to keep the snapshots for a set number of days. Usually 3 days is long enough, but sometimes the request is to keep for longer. I know I can take a snapshot using a PowerCLI script, and schedule it daily from a scheduled task. But can I automate the deletion of any snapshot over 3 days old?
I'm a PowerCLI novice, and don't currently have the skills to pull this off. So any help here would be greatly appreciated.
Thanks in advance,
Geob
Geob,
to delete all the snapshots that are older then 3 days you can do the following:
Get-VM | ` Get-Snapshot | ` Where-Object { $_.Created -lt (Get-Date).AddDays(-3) } | ` Remove-Snapshot
If you save this script as C:\Remove-SnapshotsOlderThen3Days.ps1 you can schedule it if you run the next Windows command:
%SystemRoot%\system32\windowspowershell\v1.0\powershell.exe -PSConsoleFile "C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\vim.psc1" -command "&{C:\Remove-SnapshotsOlderThen3Days.ps1}"
Robert
Wow that looks great. Is there a way to specify the VM instead of looking at all VM snapshots?
If you change "Get-VM" into "Get-VM -Name YourVMName" you get only one VM instead of all. Before you can use these commands you have to connect to a vCenter or ESX server with the Connect-VIserver cmdlet. To make the code complete for one VM it will be:
Connect-VIServer -Server YourvCenterServerName Get-VM -Name YourVMName | ` Get-Snapshot | ` Where-Object { $_.Created -lt (Get-Date).AddDays(-3) } | ` Remove-Snapshot
Robert
This script is great, I am just having no luck getting it to run as a scheduled task on Windows 2008 R2. I tried to run the script from a command prompt, then within powershell, and no luck as I keep getting an error about the 'operand' and it appears to be caused by the -command portion.
I saved the snapshot deletion script to a .ps1 file
Then I setup a scheduled task and entered in the command that was in one of the replies to this thread, then I tried to edit it, searched quite a while and no luck.
Any ideas what's missing?
Here is the command I was using and I am thinking that it's a syntax error with " marks or something else, but I am not finding it..I tried putting the spaces in for the "C:\Program Files (x86) and that didn't work, and I tried taking out the -command portion.
%systemroot%\system32\windowspowershell\powershell.exe\v1.0\powershell.exe PSConsoleFile "C:\Program Files(x86)\VMware\Infrastructure\vSphere PowerCLI\vim.psc1" -command "&{C:\Program Files(x86)\VMware\Infrastructure\vSphere PowerCLI\ps\DelSnaps.ps1}"
(Hoping that this post isn't dead)
When I ran this script from the run line (as mention in the earlier post) the Powershell window came up and I was prompted to confirm the deletion of the snapshots by entering "Y" or "A" (for yes to all). After entering "A" the script completed and my snapshots were gone...
However, When I attempted to run as a scheduled task, The task hung and did not complete. I could see the Powershell app running in the Task Manager processes and the scheduled task stated "running" but since no Powershell window popped up. I'm guessing it still needed a confirmation to delete snapshots and therefore wouldn't complete.
What would be the syntax for in the script to have the confirmation built in?
Thx for any help that can be provided.
Add the -Confirm:$false parameter
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Awesome!!! That parameter did the trick for the auto confirmation.
Thanks so much for your Help LucD!
Can you also suggest how to set and get mail confirmation after delete the snapshot automatically..
You can send mail with the Send-MailMessage cmdlet.
If that is what you are asking.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks LucD....
Thanks LucD. How do I create schedule snapshot for specific Servers. Eg., Client is requesting to Create Snapshot for 10 Servers at 30th June 2017 at 9:00 PM EST. How do i schedule snapshot for those 10 servers ?
Regards
N. Sathish
You could create a Scheduled Task for each snapshot.
See for example 1. Re: Schedule Snapshot with PowerCli
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi Robert,
Can you please add the mailing part to the script as well and write out the full script again will be very helpful.
Re: Schedule snapshot creation and removal?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi LucD,
Suppose if I have to put an exception on a VM, how to include that.??
Do you mean 'exclude' one or more VMs?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Yup, Suppose the script is for deletion of snapshots automated which is over 3 days but suppose we got an adhoc request to keep on VM snapshot for 10 days. At that case how should we exclude that vm from snapshot deletion.
You can use another Where-clause
Where-Object {$_.Name -ne 'ExcludeThisVM'} |
Get-Snapshot |
Where-Object { $_.Created -lt (Get-Date).AddDays(-3) } |
Remove-Snapshot
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks LucD.
I had created the script. I have mnay types of exceptions required so I did lots of if and else.
Script is working superb.