Last active
February 6, 2024 20:02
-
-
Save Rugby-Ball/9458891b2a41c9d60495f11b056974fd to your computer and use it in GitHub Desktop.
Get the count of Snapshots for an AWS account, along with count over set days old, and what is oldest date of snapshot and total Snapshot Volume. #AWS #Snapshots #Utility #Public #AWS_ELB
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # aws-ec2-snapshot-count-aged-out-and-totalvolume.ps1 | |
| <# | |
| Description: Get the count of Snapshots for an AWS account, along with count over set days old, and what is oldest date of snapshot and total Snapshot Volume. | |
| This WILL NOT delete anything, it is just informational on snapshot counts and aged out counts. | |
| Written: Ed Walsh | |
| PowerShell.Core tested: Yes | |
| MS-Graph: No | |
| Version: 1.2.1 | |
| Create Date: 12/9/2020 | |
| Revised Date: 2/6/2024 | |
| #> | |
| Import-Module AWSPowerShell | |
| $stopWatch = [System.Diagnostics.Stopwatch]::StartNew() | |
| $stopwatch.Start() | |
| $KeepSnapshotDays = 30 | |
| #Gets local date and time and shows the UTC offset | |
| $runon = Get-Date -Format "MM/dd/yyyy HH:mm K" | |
| $accountId = (Get-STSCallerIdentity -Region us-east-1).Account | |
| $AllSnapshots = Get-EC2Snapshot -OwnerID $accountId | |
| # | |
| Write-Output `r`n"Run on: $runon" "For AWS Account: $accountId" "Total number of snapshots: $($AllSnapshots.Count)" | |
| $AllSnapshotsForRemoval = $AllSnapshots | where StartTime -lt (Get-Date).AddDays(-$KeepSnapshotDays) | Select StartTime, SnapshotId, VolumeId, VolumeSize | |
| Write-Output "Snapshots older than $KeepSnapshotDays days: $(($allSnapshotsForRemoval |Measure-Object).Count)`nTotal Volume Size(GB): $(($allSnapshotsForRemoval.VolumeSize) |Measure-Object -Sum | select-object -expandproperty sum) `nOldest is: $(($allSnapshotsForRemoval.StartTime) |Measure-Object -Minimum | select-object -expandproperty minimum)`n" | |
| $stopwatch.Stop() | |
| $time = $stopwatch.Elapsed | |
| Write-Host "Script finished on $(Get-Date -f "MM-dd-yyyy hh:mm tt"), Elapsed time to run script (HH:MM:SS.MS): $Time" -ForegroundColor Red -BackgroundColor Yellow |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment