Created
September 4, 2019 21:28
-
-
Save leandrosilva/c463441588f864fb822e4cdf36167b98 to your computer and use it in GitHub Desktop.
PowerShell script to quick list running EC2 instances based on a name pattern
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
param ( | |
[string] $NamePattern = "Dummy-web-app-*", | |
[string] $Status = "Running" | |
) | |
function Get-InstanceStatus () { | |
$res = $(aws ec2 describe-instances --region us-east-1 --filters "Name=tag:Name,Values=$NamePattern") | |
$json = $res | ConvertFrom-Json | |
$instances = $json.Reservations.Instances ` | |
| Where-Object { $_.State.Name -eq $Status } ` | |
| Select-Object InstanceId, ` | |
InstanceType, ` | |
PublicDnsName, ` | |
@{Label="Tags"; Expression={$_.Tags | Where-Object { $_.Key -eq "Name"}}}, ` | |
LaunchTime | |
return $instances | |
} | |
### | |
"Listing EC2 instances:" | |
"- NamePattern: $NamePattern" | |
"- Status: $Status" | |
Get-InstanceStatus |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment