Steve Kehrer
Steve Kehrer

Reputation: 76

Select specific values from an array within an array

I am querying my storage array to gather properties for initiators. Here is the code:

$global:Xtrem_HBA_list = @()
$global:Xtrem_HBA_list += Get-XtremInitiators -Properties name,port-address,ig-id
$Write-Host $global:Xtrem_HBA_list

This returns

name                 port-address             ig-id
----                 ------------             -----
comp-esxi-01_vmhba2  10:00:00:90:fa:53:f4:60  {c832425d03f84644be37ae3d4e49186c, comp-esxi-01, 1}
comp-esxi-01_vmhba3  10:00:00:90:fa:53:f4:61  {c832425d03f84644be37ae3d4e49186c, comp-esxi-01, 2}

My problem is that I need $global:Xtrem_HBA_list to contain the value from name, port-address and the second value from ig-id (comp-esxi-01). I am having difficulty extracting the value from the array within the array.

Thanks to Matt! I updated my code and got the exact output I was looking for.

$global:Xtrem_HBA_list = @()
$global:arr_HBAs = @()
$global:Xtrem_HBA_list += (Get-XtremInitiators).name
foreach ($i in $global:Xtrem_HBA_list){
    $global:arr_HBAs += Get-XtremInitiator -InitiatorName $i | Select name,port-address,@{Name="ig-id";Expression={($_."ig-id")[1]}}
}

Upvotes: 0

Views: 2469

Answers (1)

Matt
Matt

Reputation: 46700

Couple basic ways to get this.

  • A loop that extracts the value from the array at every pass.
  • A loop that uses Add-Member to create another property that 'extracts' the information and use Select to pull that column.
  • Using a calculated property that will output the ig-id column but with only the second value from your array. This one is also easier as it will let us use the same name.

I'm opting for the latter suggestion. I am not using write-host on purpose here as it will just go to the output stream by default whereas write-host will just go to console

$global:Xtrem_HBA_list |  Select name,port-address,@{Name="ig-id";Expression={($_."ig-id")[1]}}

Note that if there is less than two elements a null will be returned. In that same scenario it could generate an error depending on your strict mode. Your mileage will vary but you can easily harden that with some if statements if need be.

Upvotes: 2

Related Questions