1

我有一个包含许多元素的数组$array,一个示例如下所示:

ProfileID         : 100
UID               : 17
Name              : SharePoint
Description       : SharePoint Server Description

现在我正在尝试按Name属性过滤,要匹配的字符串是:

$string
SharePoint Policy Assignment

我努力了:

$array | Where-Object {$_.Name -like "$string"}
no match

$array | Where-Object {$_.Name -like "$string*"}
no match

$array | Where-Object {$_.Name -match "$string"}
no match

这可能使用 PowerShell 吗?我错过了什么?

4

2 回答 2

1

PowerShell 中的-like运算符用于通配符匹配,因此您需要使用通配符、星号、*.

想象一下这种情况,我正在尝试匹配特定的 Windows 服务。

$svcs = Get-Service | Select-Object -first 15

C:\temp\blog> $svcs

Status   Name               DisplayName                           
------   ----               -----------                           
Stopped  AJRouter           AllJoyn Router Service                
Stopped  ALG                Application Layer Gateway Service     
Running  AMD External Ev... AMD External Events Utility           
Stopped  AppIDSvc           Application Identity                  
Running  Appinfo            Application Information               
Stopped  AppMgmt            Application Management                
Stopped  AppReadiness       App Readiness                         
Stopped  AppVClient         Microsoft App-V Client                
Stopped  AppXSvc            AppX Deployment Service (AppXSVC)     
Stopped  aspnet_state       ASP.NET State Service                 
Stopped  AssignedAccessM... AssignedAccessManager Service         
Running  AsSysCtrlService   ASUS System Control Service           
Running  AudioEndpointBu... Windows Audio Endpoint Builder        
Running  Audiosrv           Windows Audio                         
Running  AUEPLauncher       AMD User Experience Program Launcher  

要使用-Like运算符来获得匹配,我必须提供一个通配符,像这样。

$svcs | Where-Object Name -like App*

Status   Name               DisplayName                           
------   ----               -----------                           
Stopped  AppIDSvc           Application Identity                  
Running  Appinfo            Application Information               
Stopped  AppMgmt            Application Management                
Stopped  AppReadiness       App Readiness                         
Stopped  AppVClient         Microsoft App-V Client                
Stopped  AppXSvc            AppX Deployment Service (AppXSVC)     

尝试使用通配符进行操作,我敢打赌它会起作用 :)

我注意到的另一件事是,您$string的等于SharePoint Policy Assignment,但您要比较的列.NameSharePoint

于 2019-02-07T16:11:24.150 回答
1

为了补充FoxDeploy 的有用答案

对于已经在内存中或很容易适应的集合,您可以使用成员枚举来获得更方便的语法,从而加快执行速度:

@($array.Name) -like $string  # returns sub-array of matching elements

-like,当给定一个数组作为 LHS 时,充当过滤器:仅返回与 RHS 上的通配符表达式匹配的那些数组元素(也作为数组)。

请注意需要@(...)确保它$array.Name是一个数组,因为单元素数组会导致.Name属性作为标量(单个字符串)返回,在这种情况下-like将返回布尔值$true$false)而不是充当过滤器


另请注意,许多 PowerShell cmdlet直接支持通配符表达式作为参数值

Get-Service个例子,它的(隐含的)-Name参数支持通配符:

Get-Service *router*  # returns all services whose Name contains "router"

确定给定 cmdlet 参数的通配符支持

PS> Get-Help Get-Service -Parameter Name

-Name <String[]>
    Specifies the service names of services to be retrieved. Wildcards are permitted. By default, this cmdlet gets all of the services on the computer.

    Required?                    false
    Position?                    1
    Default value                None
    Accept pipeline input?       True (ByPropertyName, ByValue)
    Accept wildcard characters?  false

应该是表示支持通配符表达式的Accept wildcard characters?值,但是不幸的是,这并不可靠,因此还要检查参数说明;在这里,描述部分提供了信息。此 GitHub 问题描述了该问题,并要求使通配符支持的编程可发现性可靠。trueWildcards are permitted

于 2019-02-07T17:03:56.773 回答