Reputation: 343
Get-EC2Instance |%{ $_.RunningInstance } |
select-object InstanceId,LaunchTime,@{Name='Value'; Expression={$_.Tag.Value} }, @{Name='Key'; Expression={$_.Tag.Key} }
Each Value and Key have multiple values as you can see in the screenshot. How to rewrite the code so the output can look like:
Upvotes: 0
Views: 1070
Reputation: 24470
Here's one solution; it's not the most elegant, but hopefully solves your problem:
For Your Use Case
Get-EC2Instance | `
%{
$x = $.RunningInstance;
$x.Tag | select-object
@{Name="InstanceId"; Expression={$x.InstanceId}}
,@{Name="LaunchTime"; Expression={$x.LaunchTime}}
,@{Name="Value"; Expression={$_.Value}}
,@{Name="Key"; Expression={$_.Key}};
}
Simple Demo
cls
$x = @(
(New-Object –TypeName PSObject –Prop @{Name='one';List=@('a','b','c');})
,(New-Object –TypeName PSObject –Prop @{Name='two';List=@('d','e','f');})
,(New-Object –TypeName PSObject –Prop @{Name='three';List=@('g','h','i');})
,(New-Object –TypeName PSObject –Prop @{Name='four';List=@('j','k','l');})
,(New-Object –TypeName PSObject –Prop @{Name='five';List=@('m','n','o');})
)
#show what the preparation code produced:
#$x | select Name, List
#show the output we're after
$x | %{$n=$_.Name; $_.List | select @{Name="Name";Expression={$n}},@{Name="ListValue";Expression={$_}}}
Upvotes: 1