Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add new property based on a different objects property

I'm trying to search through one column in each row of the table. I would then like to add another value to the row based on the number being search.

This code produces the table:

$LUNSSummary = ($NY_LUNS)  -split '\s+(?=LOGICAL UNIT NUMBER)' | foreach {
    $Stringdata = $_.replace(':','=')
    New-Object PSObject -Property $(ConvertFrom-StringData $Stringdata)
}

$LUNSSummary |
    select 'Name','LOGICAL UNIT NUMBER','State','LUN Capacity(Megabytes)','LU Storage Groups' |
    Format-Table -AutoSize

enter image description here

Then I have this code which can search using the "Logical Unit Number" and produce the desired output. In this example the -contains is 1029 from the above screenshot.

$data = $LUNS_in_Pools | Out-String
$pools = $data -replace ': +','=' -split "`r`n`r`n" |
  % { New-Object -Type PSCustomObject -Property (ConvertFrom-StringData $_) } |
  select -Property *,@{n='LUNs';e={$_.LUNs -split ', '}} -Exclude LUNs

$pools | ? { $_.LUNs -contains 1029 } | select -Expand 'Pool Name'

Which produces in this case "Pool 2". The result can be Pool 1-99.

enter image description here

I want to combine these two codes to search every "Logical Unit Number" and add the result to the end of the table in a 5th section/column "Pools".

EDIT

As requested, raw data:

  • $NY_LUNS before $LUNSSummary gets it: http://pastebin.com/5wrd51Lf
  • $LUNS_in_Pools raw data: http://pastebin.com/Zg9q6jhe

Desired Output: (Pool is obtained from "Logical Unit Number") enter image description here

EDIT 2

This is now the closest to correct so far, it prints the same pool result every time.

$LUNSSummary = 
($NY_LUNS)  -split '\s+(?=LOGICAL UNIT NUMBER)' | 
foreach { $Stringdata = 
          $_.replace(':','=')
          New-Object PSObject -Property  $(ConvertFrom-StringData $Stringdata)
} 

$data = $LUNS_in_Pools | Out-String
$pools = $data -replace ': +','=' -split "`r`n`r`n" |
  % { New-Object -Type PSCustomObject -Property (ConvertFrom-StringData $_) } |
  select -Property *,@{n='LUNs';e={$_.LUNs -split ', '}} -Exclude LUNs

$poolProperty = @{Label="Pool";Expression={$pools | ? { $_.LUNs -contains       [int]$_.'LOGICAL UNIT NUMBER'} | select -Expand 'Pool Name'}}
$LUNSSummary | select 'Name','LOGICAL UNIT NUMBER','State','LUN      Capacity(Megabytes)','LU Storage Groups',$poolProperty

if I check the output of $pools | ? { $_.LUNs -contains [int]$_.'LOGICAL UNIT NUMBER'} | select -Expand 'Pool Name'

I only see one result. I'm thinking maybe it has to be looped some how?

like image 510
RichT Avatar asked Nov 28 '25 11:11

RichT


1 Answers

From the guess of it you just need one more calculated property on the end there for 'Pool'. You already have, and tested, the logic. Just need to implement it.

$poolProperty = @{Label="Pool";Expression={
    $lunID = $_.'LOGICAL UNIT NUMBER';
    $pools | Where-Object{$_.LUNs -contains $lunID} | 
        Select-Object -Expand 'Pool Name'}
}
$LUNSSummary | select 'Name','LOGICAL UNIT NUMBER','State','LUN Capacity(Megabytes)','LU Storage Groups',$poolProperty

We take the LOGICAL UNIT NUMBER of the current item in the pipeline and save it so that we can start another to extract the match from the $pools object. As long as you luns are exclusive this would always return one Pool Name.

The above should work but I changed how $pools was created so it matched the logic of $LUNSSummary. I used here-strings for the raw data from your paste bin.

$LUNSSummary = ($NY_LUNS)  -split '\s+(?=LOGICAL UNIT NUMBER)' | 
foreach { $Stringdata = 
          $_.replace(':','=')
          New-Object PSObject -Property  $(ConvertFrom-StringData $Stringdata)
} 

$pools = ($LUNS_in_Pools | Out-String) -split '\s+(?=Pool Name)' | ForEach-Object{
    New-Object -Type PSCustomObject -Property (ConvertFrom-StringData ($_ -replace ":","=")) |
    Select -Property *,@{n='LUNs';e={$_.LUNs -split ',\s*'}} -Exclude LUNs
}

$poolProperty = @{Label="Pool";Expression={
    $lunID = $_.'LOGICAL UNIT NUMBER';
    $pools | Where-Object{$_.LUNs -contains $lunID} | 
        Select-Object -Expand 'Pool Name'}
}
$LUNSSummary | select 'Name','LOGICAL UNIT NUMBER','State','LUN Capacity(Megabytes)','LU Storage Groups',$poolProperty

Looks like $LUNS_in_Pools was a newline delimited string. Piping to Out-String cleaned it up to remove the newlines and allow the regex/ConvertFrom-StringData to work.

like image 194
Matt Avatar answered Dec 01 '25 08:12

Matt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!