Get-EC2Instance |%{ $_.RunningInstance } |
select-object InstanceId,LaunchTime,@{Name='Value'; Expression={$_.Tag.Value} }, @{Name='Key'; Expression={$_.Tag.Key} }
每个值和键都有多个值,如屏幕截图所示。如何重写代码,使输出看起来像:
答案 0 :(得分:1)
这是一个解决方案;它不是最优雅的,但希望能解决你的问题:
适合您的使用案例
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}};
}
简单演示
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={$_}}}