Add a new property based on another object
I am trying to search one column in each row of a table. Then I would like to add another value to the string based on the number being searched.
This code creates a 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
Then I have this code that can search using "Logical Device Number" and produce the desired output. In this example, the content contains 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'
What does Pool 2 produce in this case? The result could be pool 1-99.
I want to combine these two codes to find each "LUN" and add the result to the end of the table in the fifth section / column "Pools".
EDIT
As requested, the raw data is:
-
$NY_LUNS
before$LUNSSummary
gets it: http://pastebin.com/5wrd51Lf -
$LUNS_in_Pools
initial data: http://pastebin.com/Zg9q6jhe
Desired result: (pool derived from "LUN number")
EDIT 2
Now this is the closest to a fix so far, it prints the same pooling 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 $pools | ? { $_.LUNs -contains [int]$_.'LOGICAL UNIT NUMBER'} | select -Expand 'Pool Name'
I only see one result. I think maybe it should be looped in some way?
source to share
On the assumption of this, you just need another calculated property at the end for the "Pool". You already have and tested your logic. You 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
current item in the pipeline and store it so we can start at a different one to retrieve the match from the object $pools
. As long as you luns
are exclusive, this always returns one Pool Name
.
The above should work, but I changed the way of creation $pools
to fit the logic $LUNSSummary
. I've used strings here for the raw data from your insert container.
$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 it $LUNS_in_Pools
was a newline-delimited string. The pipeline has Out-String
cleared it up to remove newlines and allow regex
/ work ConvertFrom-StringData
.
source to share