使用计算所得的属性时,Group-Object显示一个哈希表 [英] Group-Object is showing a hashtable when using calculated properties

查看:111
本文介绍了使用计算所得的属性时,Group-Object显示一个哈希表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景

从预定义的服务器名称列表中,我正在搜索配置文件,并尝试将每个服务器的所有文件归为一组.文件名可以映射到应用程序.

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.

问题

分组部分给我带来了问题.它一直返回哈希表,我只想返回文件名.

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

SSCCE 注意:分组位于sscce中的文件名上,但问题的要点是相同的

此示例显示了找到匹配项的整个行,而不仅仅是匹配项

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;以下是我目前正在使用的

@(
  "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

这将返回类似的内容

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

我想让它返回的地方

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

推荐答案

这是否给出您想要的内容(替换代码的最后一行):

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}}

并摆脱散列/花括号:

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

这篇关于使用计算所得的属性时,Group-Object显示一个哈希表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆