Reputation: 361
I need some help on this:
I've got a [pscustomobject] that contains the below results, get from a xml file
Files Session
----- -------
{IMG_0518.JPG, IMG_0520.JPG, VG_Overview.mp4, } VG_Overview
I need to get the duplicated values on Files, e get it's respective session value:
My code to get the duplicated values: $trse is my [pscustomobject]
$trse | ForEach-Object {$_.Files.Where({ $_ -ne ""})} | Group | Where-Object{$_.Count -gt 1} | Select @{Name="Files";Expression={$_.Name}},@{Name="Session";Expression={$_.Session}},@{Name="Occurrences";Expression={$_.Count}}
and the results are this:
Files Session Occurrences
----- ------- -----------
GC7_Gender.WAV 2
NG6_Gender.WAV 2
NG9_Gender_I.WAV 2
NG9_Gender_II.WAV 2
VG4_Gender.WAV 2
And i need to get this:
Files Session Occurrences
----- ------- -----------
GC7_Gender.WAV VG_Over 2
NG6_Gender.WAV Gh_tre 2
NG9_Gender_I.WAV FG_iop 2
NG9_Gender_II.WAV VB_jkl 2
VG4_Gender.WAV ER_uilv 2
Thanks a lot for any help on this, i think the problem is when i get de duplicated files, i lost the session values.
Upvotes: 0
Views: 100
Reputation: 175085
This is probably easier to understand if we re-format your code slightly:
$trse | ForEach-Object {
$_.Files.Where( { $_ -ne "" } )
} | Group | Where-Object { $_.Count -gt 1 } | Select @{Name = "Files"; Expression = { $_.Name } }, @{Name = "Session"; Expression = { $_.Session } }, @{Name = "Occurrences"; Expression = { $_.Count } }
ForEach-Object
outputs only the Files
values, so by the time we reach Group
, any relationship between the file names and the corresponding Session
value has been lost.
Move the Group | Where | Select
chain inside the ForEach-Object
block:
$trse | ForEach-Object {
$Session = $_.Session
$_.Files.Where( { $_ -ne "" } ) | Group | Where-Object { $_.Count -gt 1 } | Select @{Name = "Files"; Expression = { $_.Name } }, @{Name = "Session"; Expression = { $Session } }, @{Name = "Occurrences"; Expression = { $_.Count } }
}
For the calculated property expressions that just rename existing properties, you can supply the input property name as the Expression
value, no need to supply a scriptblock:
... |Select @{Name = "Files"; Expression = 'Name' }, @{Name = "Session"; Expression = { $Session } }, @{Name = "Occurrences"; Expression = 'Count' }
Upvotes: 4