按属性名称过滤 cim 实例类

Lig*_*War 2 powershell

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

Get-CimInstance -ClassName Win32_Product -Property Name -Filter "Microsoft*"
Run Code Online (Sandbox Code Playgroud)

返回错误:Get-CimInstance : Invalid query

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

Get-CimInstance -ClassName Win32_Product | ? {$_.Name -like 'Microsoft*'}
Run Code Online (Sandbox Code Playgroud)

但没有管道到Where-Object.

我究竟做错了什么?

Viv*_*ngh 5

如果你看一下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
Run Code Online (Sandbox Code Playgroud)

您不必在Where-Object此处包含,并且需要将代码编写为查询。该-filter参数将采用Property(Name in this case)的形式Windows Query Language-Property使用参数时不需要显式定义该参数-filter。此外,由于您正在使用WQL,您的通配符搜索将从 变为*%与 中的情况非常相似SQL。记住这些点,您可以使用以下查询 -

 Get-CimInstance -ClassName Win32_Product -Filter 'Name like "Microsoft%"'
Run Code Online (Sandbox Code Playgroud)