首页 > 解决方案 > Powershell - 合并两个 Note 属性

问题描述

如何在 powershell 中将两个 Note 属性合并在一起?

以下查询返回实例列表,但我想合并注释属性,如下所示。

$((Get-Counter '\Process(Chrome*)\ID Process' -ErrorAction SilentlyContinue).CounterSamples | % {[regex]$a = "^.*\($([regex]::Escape($_.InstanceName))(.*)\).*$";[PSCustomObject]@{InstanceName=$_.InstanceName;InstanceId=$a.Matches($($_.Path)).groups[1].value}}) 

查询的输出是:

InstanceName                                                InstanceId
------------                                                ----------
Chrome                                                      #2
Chrome                                                      #1
Chrome   

但我希望输出是这样的..

InstanceName
------------ 
Chrome#2
Chrome#1
Chrome   

标签: powershell

解决方案


您只需将第一个属性的表达式值更改为:

@{InstanceName="$($_.InstanceName)$($a.Matches($($_.Path)).groups[1].value)"}})

在全:

(Get-Counter '\Process(Chrome*)\ID Process' -ErrorAction SilentlyContinue).CounterSamples | % {[regex]$a = "^.*\($([regex]::Escape($_.InstanceName))(.*)\).*$";[PSCustomObject]@{InstanceName="$($_.InstanceName)$($a.Matches($($_.Path)).groups[1].value)"}}

推荐阅读