Group-Object is showing a hashtable when using cal

2019-09-17 05:08发布

问题:

Background

From list of predefined servernames, I'm searching in config files and trying to group for each server all the files where it's found. A filename can be mapped to an application.

Problem

The grouping part is giving me problems. It keeps returning a hashlist where I just want the filename returned.

SSCCE note: grouping is on filename in the sscce but the gist of the problem is the same

This example is showing the entire line where the match is found instead of just the match

@("A test", "Another test") | sls -pattern "\btest\b" | group filename | select name, group

Trying to adjust for that, I've added a calculated property to only get the match. Unfortunately, this shows the group as a Hashlist.

@("A test", "Another test") | sls -pattern "\btest\b" | select filename, @{n="Server";e={$_.matches[0].Value}} | group filename | select name, group

TLDR; following is what I'm currently using

@(
  "server1"
  , "server2"
) | 
% {sls -path "C:\PrivateWS\sources\confdoc\applicaties\*.yml" -Pattern "\b$($_)\b"} | 
select Filename, @{n="Server";e={$_.matches[0].Value}} | 
group server | select name, group

and this returns something like this

Name    Group                                                                                                                                                                                              
----    -----                                                                                                                                                                                              
server1 {@{Filename=applicationX.yml; Server=server1}, @{Filename=applicationY.yml; Server=server1}}                                                                                 
server2 {@{Filename=applicationX.yml; Server=server2}}

where I would like to have it return this

Name    Group                                                                                                                                                                                              
----    -----                                                                                                                                                                                              
server1 {applicationX.yml, applicationY.yml}                                                                                 
server2 {applicationX.yml}

回答1:

Does this give what you want (replacement for the last line of your code):

group server | select name, @{name='group'; expression={$_.group | Select -ExpandProperty Filename}}

And to get rid of the hash/curly braces:

group server | select name, @{name='group'; expression={(@($_.Group | Select -ExpandProperty Filename)) -join ', '}}


回答2:

What you see there is the string representation of custom objects, not hashtables. You're getting that, because Group-Object doesn't group just one property of the input objects, but the whole objects.

I think what you actually want is convert your list of groups to a hashtable:

$ht = @{}
'server1', 'server2' |
    ForEach-Object { Select-String ... } |
    Select-Object Filename, @{n="Server";e={$_.matches[0].Value}} |
    Group-Object server |
    ForEach-Object { $ht[$_.Name] = $_.Group | Select-Object -Expand Filename }