1

I'm working on a small script that will get all the snapshots in a VM and remove all snapshots in the VM except for the 6 newest snapshots, based off the description.

Right now my code looks like this:

get-snapshot -vm "test" | sort -property description | remove-snapshot ?

I am using get-date to make the description for each VM be the date on which it was created, and want to remove all but the 6 newest snapshots. What am I missing with my script to accomplish this task?

I was thinking of using the -getchildren however I'm unable to figure out how to get it to where it would delete snapshots 7 and on.

4

1 回答 1

2

A snapshot has a property called Created so you can sort on this property and skip the first 6. Test this in a test environment and remove the WhatIf switch to delete the snapshots.

Get-Snapshot -VM test | 
Sort-Object Created | 
Select-Object -Skip 6 | 
Remove-Snapshot -Confirm:$false -WhatIf
于 2012-07-10T07:19:51.140 回答