Skip to content

Instantly share code, notes, and snippets.

@bluPhy
Last active August 31, 2021 11:09
Show Gist options
  • Save bluPhy/5a1079f48c65f0d1922abb91a2ba5b8d to your computer and use it in GitHub Desktop.
Save bluPhy/5a1079f48c65f0d1922abb91a2ba5b8d to your computer and use it in GitHub Desktop.
AWS-ListAllAMIsInUse.ps1
Function Get-AWSModule {
Param([string]$name)
if (-not(Get-Module -Name $name)) {
if (Get-Module -ListAvailable | Where-Object { $_.name -eq $name }) {
Import-Module -Name $name
$true
} #end if module available then import
else { $false } #module not available
} # end if not module
else { $true } #module already loaded
} #end function get-AWSModule
Get-AWSmodule -name “AWSPowerShell.NetCore”
# This script list all AMIs in use in all regions in an account
$Regions = (Get-AWSRegion | Sort-Object -Property Region)
$Regions | ForEach-Object -Process {
Write-Host "Listing region: " $_.Region "..."
Try { $AMIs = ((Get-EC2Instance -Region $_.Region ).Instances.ImageId) | Sort-Object -Unique }
Catch { Write-Host "Could not access region" $_.Region }
If ($AMIs -ne $null) {
Write-Host "Listing" $AMIs.Count "AMIs in region:" $_.Region "..."
Get-EC2Image -ImageId $AMIs -Region $_.Region | Format-Table ImageId, ImageLocation, Name
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment