Skip to content

Instantly share code, notes, and snippets.

@reecestart
Created January 9, 2018 04:32
Show Gist options
  • Save reecestart/7e3d3c59744a1d1107db54fce29a22ad to your computer and use it in GitHub Desktop.
Save reecestart/7e3d3c59744a1d1107db54fce29a22ad to your computer and use it in GitHub Desktop.
cmdlet that can list all running ec2 instances with tags in table format
$Regions = Get-EC2Region
$Regions = $Regions.RegionName
$RunningInstances = @()
foreach ($Region in $Regions)
{
$EC2Instances = Get-EC2Instance -Region $Region
foreach ($EC2Instance in $EC2Instances)
{
if ($EC2Instance.Instances.state.name.value -eq 'running')
{
$RunningInstances += $EC2Instance
}
}
}
$EC2InstancesWithTagsArray = @()
foreach ($RunningInstance in $RunningInstances)
{
$myobj = "" | Select "InstanceId","AZ","Tags"
$myobj.InstanceId = $RunningInstance.Instances.InstanceId
$myobj.AZ = $RunningInstance.Instances.Placement.AvailabilityZone
$myobj.Tags = $RunningInstances.Instances.Tag
$EC2InstancesWithTagsArray += $myobj
$myobj = $null
}
$EC2InstancesWithTagsArray | Format-Table
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment