String 如何使用Where对象匹配属性字符串的任何部分

String 如何使用Where对象匹配属性字符串的任何部分,string,powershell,wildcard,member-enumeration,String,Powershell,Wildcard,Member Enumeration,我有一个包含许多元素的数组$array,示例如下: ProfileID : 100 UID : 17 Name : SharePoint Description : SharePoint Server Description 现在我尝试按Nameproperty进行筛选,要匹配的字符串是: $string SharePoint Policy Assignment 我试过: $array | Where-Obje

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

ProfileID         : 100
UID               : 17
Name              : SharePoint
Description       : SharePoint Server Description
现在我尝试按
Name
property进行筛选,要匹配的字符串是:

$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?我缺少什么?

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策略分配
,但您在
.Name
上比较的列只是
SharePoint
来补充:

对于已在内存中或易于安装的集合,您可以使用获得更方便的语法,从而加快执行速度:

@($array.Name) -like $string  # returns sub-array of matching elements
-类似于
,当给定一个数组作为LHS时,充当过滤器
:仅返回与RHS上通配符表达式匹配的数组元素(也作为数组)

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


还请注意,许多PowerShell 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
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
PS>获取帮助获取服务-参数名称
-名字
指定要检索的服务的服务名称。允许使用通配符。默认情况下,此cmdlet获取计算机上的所有服务。
必修的?假的
位置?1.
默认值无
接受管道输入?True(ByPropertyName,ByValue)
是否接受通配符?假的
表示支持通配符表达式的值应该是
接受通配符?
true
,但是,不幸的是,这不可靠,因此还要检查参数说明;此处,允许使用描述符部分
通配符
提供信息。

描述此问题并要求使通配符支持的编程可发现性可靠。

假设
$string='share'
$array |其中对象{$\u.Name-类似于“$string*”}
对我有用。@beatcracker
$string='SharePoint策略分配'
在对象
name
属性中不起换行或奇怪的作用?@LightningWar,您的属性不匹配!数组中的“名称”列只是SharePoint,而字符串是SharePoint策略分配。