minisch
minisch

Reputation: 343

Powershell Split items in a string

I cant figure out how this works...please help...

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

This command displays : enter image description here

I run the second command below which splits "Key" (Each instanceId has multiple Keys associated to it. Each key has its associated value):

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

The output is enter image description here

How to modify the second command to include "Value" column (each key has a associated value)?

Upvotes: 0

Views: 87

Answers (2)

Keith Hill
Keith Hill

Reputation: 201952

I "think" you want this:

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

Upvotes: 1

JPBlanc
JPBlanc

Reputation: 72680

I'am not sure to follow but if I understand :

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

Then

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

I can't test it ans it's very early in the morning so be lenient.

Upvotes: 0

Related Questions