Powershell将字符串中的项目拆分

时间:2015-06-24 01:54:46

标签: powershell amazon-web-services

我无法弄清楚这是如何运作的......请帮助......

$output = Get-EC2Instance |%{ $_.RunningInstance } | select-object InstanceId,@{Name='Key'; Expression={$_.Tag.Key} },@{Name='Value'; Expression={$_.Tag.Value} }

此命令显示: enter image description here

我运行下面的第二个命令,它拆分“Key”(每个instanceId都有多个与之关联的键。每个键都有相关的值):

$output  | %{$n=$_.InstanceId; $_.Key | select @{Name="InstanceID";Expression={$n}},@{Name="Key";Expression={$_}}}

输出是 enter image description here

如何修改第二个命令以包含“Value”列(每个键都有一个关联值)?

2 个答案:

答案 0 :(得分:1)

我“想”你想要这个:

$output | Foreach {$vals=$_.Value; $n=$_.InstanceId; $i = 0; $_.Key |  
                   Select @{Name="InstanceID";Expression={$n}},
                          @{Name="Key";Expression={$_}},
                          @{Name="Value";Expression={$vals[$i++]}}}

答案 1 :(得分:0)

我不确定要遵循,但如果我明白:

$a =$output[0]
$a["one_key"] -> "One_Value"

然后

$output  | %{$n=$_; $_.Key | select @{Name="InstanceID";Expression={$($n.InstanceId)}},@{Name="Key";Expression={$_}},@{Name="Value";Expression={$n[$_]}}}

我不能在早上很早就测试它,所以要宽容。