Created
November 12, 2021 02:08
-
-
Save Rugby-Ball/d8f0bd977bc99ef2f954a3a535b62ec0 to your computer and use it in GitHub Desktop.
This will pull an inventory of all snapshots for all EBS volumes attached to an EC2 Instance #Public #AWS #Inventory #EC2 #EBS
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
| # ec2-snapshots-inventory.ps1 | |
| <# | |
| Description: This will pull an inventory of all snapshots for all EBS volumes attached to an EC2 Instance | |
| original from: https://matthewdavis111.com/powershell/ec2-latest-snapshots-of-attached-volumes/ | |
| Altered original script to fit my needs. | |
| Written by: Matthew Davis - see link above to original script. | |
| Modified by: Ed Walsh | |
| PowerShell.Core tested: Not Tested | |
| Version: 1.0.0 | |
| Create Date: 1/20/2021 | |
| Revised Date: 1/20/2021 | |
| #> | |
| import-module AWSPowerShell | |
| # Case sensitive! | |
| $InstanceName = 'Server1' #Put in Server name here. Case sensitive! | |
| # | |
| $timestamp = get-date -format yyyyMMddHHmmss | |
| $subfolder = if ( $True -eq $iswindows ) { "\Documents\" } Else { "" } | |
| $mydocuments = $home + $subfolder | |
| $fileName = "EC2-"+$InstanceName+"-EBS-Volume-Snapshot-Inventory-"+ [string]$timestamp + ".csv" | |
| $filePath = Join-Path $mydocuments $fileName | |
| <# | |
| Gets the attached volumes of the specified instance and shows the latest snapshots for each volume | |
| #> | |
| #$snapshots = @() | |
| $o = @() | |
| $out = @() | |
| # Create an EC2 filter to find the instance with the corresponding Name tag (if you don't Name your instances, use another tag value) | |
| $instanceFilter = New-Object Amazon.EC2.Model.Filter | |
| $instanceFilter.Name = 'tag:Name' | |
| $instanceFilter.Value = $InstanceName | |
| #Get the EC2 Instance ID | |
| $ec2ID = (Get-EC2Instance -Filter $instanceFilter).Instances.InstanceID | |
| # Get EC2 instance volumeIDs | |
| $volumeIDs = (Get-EC2Instance -Filter $instanceFilter).Instances.blockdevicemappings.ebs.volumeid | |
| # Use the volumeID(s) to get the latest snapshopID(s) | |
| # Get the latest snapshot for each volume | |
| foreach ($volumeID in $volumeIDs) { | |
| $snapshotFilter = New-Object Amazon.EC2.Model.Filter | |
| $snapshotFilter.Name = 'volume-id' | |
| $snapshotFilter.value = $volumeID | |
| $snapshotId = Get-EC2Snapshot -Filter $snapshotFilter #| Select-Object -First 1 | |
| #$snapshots += $snapshotId | |
| #Loop through to get each Snapshot taken for the volume. | |
| foreach ($sid in $snapshotid) { | |
| $o = New-Object -TypeName System.Management.Automation.PSObject -Property ([ordered]@{ | |
| 'Server' = $Instancename; | |
| 'Instance-ID' = $ec2ID; | |
| 'Snapshot-ID' = $sid.SnapshotId; | |
| 'Time-Stamp' = $sid.StartTime; | |
| 'Encrypted' = $sid.Encrypted; | |
| 'Volume-ID' = $sid.VolumeId; | |
| 'Volume-Size' = $sid.VolumeSize; | |
| 'State' = $sid.State; | |
| 'Description' = $sid.Description; | |
| }) | |
| $out += $o | |
| } | |
| } | |
| #$out | Out-gridview | |
| $out | Export-Csv -NoTypeInformation -Append -Path $filepath | |
| Write-Output "Exported to: $filePath" | |
| Write-Output "\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\|||||||////////////////////////////////////" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment