VMware Cloud Community
KrishnaBSR
Contributor
Contributor

Deletion of the oldest snapshots

Hi ,

Basically I just want 5 servers to keep the last 2 weekly snapshots and to have this automated so that each week it deletes the oldest snapshot.

Please suggest best script.

Thanks

Reply
0 Kudos
13 Replies
DZ1
Hot Shot
Hot Shot

You could run this, so it deletes snaphsots over 15 days old.

Get-VM | Get-Snapshot | where { ((Get-date) - $_.Created).days -gt 15 } | Remove-Snapshot -Confirm:$false -RunAsync

Reply
0 Kudos
LucD
Leadership
Leadership

Or do something like this

Get-VM | %{

   Get-Snapshot -VM $_ | Sort -Property Created -Descending | Select -Skip 2 | Remove-Snapshot -Confirm:$false

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
KrishnaBSR
Contributor
Contributor

Thank you Very Much

Reply
0 Kudos
KrishnaBSR
Contributor
Contributor

Thank you very much

Reply
0 Kudos
KrishnaBSR
Contributor
Contributor

Hi All,

Can we get the deleted snapshots report .

Thanks

Reply
0 Kudos
LucD
Leadership
Leadership

Try something like this

Get-VM | %{

   $snap = Get-Snapshot -VM $_ | Sort -Property Created -Descending | Select -Skip 2

   $snap | Remove-Snapshot -Confirm:$false

   $snap | Select @{N="VM";E={$_.Vm.Name}},Name,Created

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
KrishnaBSR
Contributor
Contributor

Thank you very much Lucd.

Scripting is running fine.

However ,I am receiving  below error while scheduling script.

E:\>C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe -psconsolefile "C:

\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\vim.psc1"  -noexit -

command  E:\SNAPSHOT.PS1

Missing closing '}' in statement block.

At E:\SNAPSHOT.PS1:1 char:159

+ Connect-VIServer -SERVER 10.10.10.5 {Get-vm | %{get-snapshot -vm $_ | Sort -

Property Created -Descending | Select -Skip 2 | Remove-Snapshot -Confirm:$false

} <<<<

    + CategoryInfo          : ParserError: (CloseBraceToken:TokenId) [], Parse

   Exception

    + FullyQualifiedErrorId : MissingEndCurlyBrace

PS E:\>

Plz suggest me solution for this issue.

Thanks & Regards,

Reply
0 Kudos
LucD
Leadership
Leadership

Perhaps you can attach the .ps1 file you are trying to schedule.

It looks like there are <CR><LF> missing, and there shouldn't be a curly brace after the Connect-VIServer line (jusding from the error message)


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
KrishnaBSR
Contributor
Contributor

Hi Lucd,

Attached script.

Thanks & Regards

Reply
0 Kudos
LucD
Leadership
Leadership

Try like this

Connect-VIServer -SERVER 10.10.10.5

Get-vm | %{get-snapshot -vm $_ | Sort -Property Created -Descending | Select -Skip 2 | Remove-Snapshot -Confirm:$false

Make sure to have the <CR><LF> in there (Get-VM starts on a new line).


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
KrishnaBSR
Contributor
Contributor

Hi Lucd,

Its was executing till connecting vCenter server.

Later its not continuing the script (Get-VM.....)

Thanks & Regards

Reply
0 Kudos
LucD
Leadership
Leadership

It looks like you are using an IE browser to copy/paste the code, and that browser is known to have issues with the forum SW.

Try the attached script (to avoid all copy/paste issues)


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

KrishnaBSR
Contributor
Contributor

Thank you very much Lucd.

I will test and get back in 8 hours.. .

Thanks & Regards,

Reply
0 Kudos