Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $error.Clear()
- $gateway = Get-SGGateway
- $volume = Get-SGVolume -GatewayARN $gateway.GatewayARN
- $volumeid = $volume.VolumeARN.Substring($volume.VolumeARN.LastIndexOf("/")+1).ToLower()
- $filter = New-Object Amazon.EC2.Model.Filter
- $filter.Name = "volume-id"
- $filter.Value.Add($volumeid)
- $today = [DateTime]::Now.Date
- $oldTimeSpan = -7
- $expiredTimeSpan = -14
- $snapshots = Get-EC2Snapshot -Filter $filter
- ## Get snapshots between 1 and 2 weeks old
- $oldSnapshots = $snapshots | Where-Object {$_.StartTime.Date -lt $today.AddDays($oldTimeSpan) -and $_.StartTime.Date -ge $today.AddDays($expiredTimeSpan)}
- ## Get snapshots older than 2 weeks
- $expiredSnapshots = $snapshots | Where-Object {$_.StartTime.Date -lt $today.AddDays($expiredTimeSpan)}
- $deleted = New-Object System.Collections.ArrayList
- $responses = New-Object System.Collections.ArrayList
- ## Remove gateway snapshots older than 2 weeks
- ForEach($snapshot in $expiredSnapshots){
- Try{
- Remove-EC2Snapshot -SnapshotId $snapshot.SnapshotId -Force
- }
- Catch{
- Continue
- }
- $deleted.Add(($snapshot | Select-Object SnapshotId, StartTime)) | Out-Null
- }
- ## Remove all snapshots except the 6am one for snapshots between 1 and 2 weeks old
- ForEach($snapshot in $oldSnapshots){
- If($snapshot.StartTime.TimeOfDay.Hours -le 5 -or $snapshot.StartTime.TimeOfDay.Hours -ge 7){
- Try{
- Remove-EC2Snapshot -SnapshotId $snapshot.SnapshotId -Force
- }
- Catch{
- Continue
- }
- $deleted.Add(($snapshot | Select-Object SnapshotId, StartTime)) | Out-Null
- }
- }
- $deleted | Export-Csv -Path "C:\Logs\AWS-Snapshots\SnapshotLogDeletions-$($today.Date.ToString("yyyyMMdd")).csv" -NoTypeInformation
- If($error){
- $error | Export-Csv -Path "C:\Logs\AWS-Snapshots\SnapshotLogErrors-$($today.Date.ToString("yyyyMMdd")).csv" -NoTypeInformation
- }
Advertisement
Add Comment
Please, Sign In to add comment