按属性名称过滤cim实例类

时间:2018-04-18 09:58:45

标签: powershell

我试图按属性名称过滤结果。我在我的脚本中无法使用管道

Get-CimInstance -ClassName Win32_Product -Property Name -Filter "Microsoft*"

返回错误:Get-CimInstance : Invalid query

我试图获得类似于此命令的输出:

Get-CimInstance -ClassName Win32_Product | ? {$_.Name -like 'Microsoft*'}

但没有管道Where-Object

我做错了什么?

1 个答案:

答案 0 :(得分:1)

如果你看Get-Help Get-CimInstance -Full,你会发现以下内容 -

-Filter [<String>]
    Specifies a where clause to use as a filter. Specify the clause in either the WQL or the CQL query language.

    Note: Do not include the where keyword in the value of the parameter.

    Required?                    false
    Position?                    named
    Default value                none
    Accept pipeline input?       True (ByPropertyName)
    Accept wildcard characters?  false

您不必在此处加入Where-Object,您需要将代码编写为查询。 -filter参数将采用Property(Name in this case)形式的Windows Query Language。在使用-Property参数时,您不需要明确提及-filter参数。此外,由于您使用的是WQL,因此您的通配符搜索会从*更改为%,与SQL中的非常相似。记住这些要点,您可以使用以下查询 -

 Get-CimInstance -ClassName Win32_Product -Filter 'Name like "Microsoft%"'
相关问题