I'm trying to take an array of PSObjects similar to
@{BakId=27; Name=DB_A; Lsn=123; File=A_01; Size=987}
@{BakId=28; Name=DB_B; Lsn=456; File=B_01; Size=876}
@{BakId=28; Name=DB_B; Lsn=456; File=B_02; Size=765}
@{BakId=28; Name=DB_B; Lsn=456; File=B_03; Size=654}
And create a new grouped object that removes redundant header info.
BakId Lsn Name Files
27 123 DB_A {@{File=A_01.bak;Size=777}}
28 456 DB_B {@{File=B_01.bak;Size=888}, @{File=B_02.bak;Size=999}, ...}
I tried using group-object but can only get it to work for one property. (all grouped properties go into Group.Name as a a string of comma separated values.)
This is the best I've come up with, but feels hacky.
$list | Group-Object -Property BakId | % {
$BakId = $_.Name
$Lsn = $_.Group[0].Lsn # <--- is there a better way than this?
$Name = $_.Group[0].Name # <--- Ditto
$Files = $_.Group | Select-Object -Property SequenceNumber, Size
Write-Output (New-Object -TypeName PSObject -Property @{ BakId=$BakId;Files = $Files })
}
Is there a better way?
Thanks