Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save Rugby-Ball/5837c22da3ed13afdfa5684d90e89543 to your computer and use it in GitHub Desktop.

Select an option

Save Rugby-Ball/5837c22da3ed13afdfa5684d90e89543 to your computer and use it in GitHub Desktop.
Modified from original to scan all AWS Regions, and allow wildcard to pull in all EC2's and associated snapshots for the EBS volumes
#ec2-snapshots-full-inventory.ps1
#This will pull an inventory of all snapshots for all EBS volumes attached to an EC2 Instance.
#Can take wildcard to pull in all EC2 instances in all regions, or an EC2 Tag:NAME value to look for only one EC2 across all regions.
#Scans all AWS Regions.
# original from: https://matthewdavis111.com/powershell/ec2-latest-snapshots-of-attached-volumes/
#Altered to fit needs.
#Altered by: Ed Walsh
#Originally Modified: 01/20/2021
#Edited: 06/24/2021
#
# Instance Name is Case sensitive! Can also use a WildCard '*' to get all EC2's and associated EBS snapshots but it could take a long time for script to run!
#$InstanceName = 'EC2ServerName1'
$InstanceName = '*'
#
$timestamp = get-date -format yyyyMMddHHmmss
$subfolder = if (($PSVersionTable.PSEdition) -eq "Core") { if ( $True -eq $iswindows ) { "\Documents\" } Else { "" } } Else {"\Documents\"}
$mydocuments = $home + $subfolder
$fileName = "EC2-"+ (&{If($InstanceName -eq '*') {"All-Instances"} Else {$instancename}}) +"-EBS-Volume-Snapshot-Inventory-"+ [string]$timestamp + ".csv"
$filePath = Join-Path $mydocuments $fileName
$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
$fullinstancefilter = "-filter $instanceFilter"
$regions = (Get-EC2Region).RegionName
foreach ($region in $regions) {
Write-Progress -Activity “Checking Region: $region”
$i = 0
$ec2IDs = (Get-EC2Instance -region $region -filter $instancefilter).Instances.InstanceID
$ec2countregion = $ec2ids | Measure-Object
$ec2count = ($ec2countregion).count
Foreach ($ec2id in $ec2ids) {
$i = $i + 1
Write-Progress -Activity “Checking Region: $region” -status “Scanning EC2 $i of $ec2count” -percentComplete ($i / $ec2count*100)
# Get EC2 instance volumeIDs
$volumeIDs = (Get-EC2Instance -region $region -InstanceId $ec2id).Instances.blockdevicemappings.ebs.volumeid
$ec2name = (Get-EC2Tag -Region $region -Filter @{Name="resource-type";Value="instance"},@{Name="resource-id";Value=$ec2id},@{Name="key";Value="Name"} | Select-Object @{Name="Name";Expression={$PSItem.Value}} ).name
# 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 -Region $region -Filter $snapshotFilter #| Select-Object -First 1 #Uncomment if you only want the current snapshot and not all of them.
#Loop through to get each Snapshot taken for the volume.
foreach ($sid in $snapshotid) {
$o = New-Object -TypeName System.Management.Automation.PSObject -Property ([ordered]@{
'Region' = $region;
'Server' = $ec2name;
'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 | 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