2

我在会话中启动了一个 notepad.exe:

gwmi -Query "Select CommandLine from Win32_Process where CommandLine='C:\Windows\system32\notepad.exe'"

Get-WmiObject : Demande non valide
Au niveau de ligne : 1 Caractère : 5
+ gwmi <<<<  -Query "Select CommandLine from Win32_Process where CommandLine='C:\Windows\system32\notepad.exe'"
    + CategoryInfo          : InvalidOperation: (:) [Get-WmiObject], ManagementException
    + FullyQualifiedErrorId : GetWMIManagementException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

我测试:

gwmi -Query "Select CommandLine from Win32_Process where CommandLine='C:\\Windows\\system32\\notepad.exe'"

它什么也没给

gwmi -Query "Select CommandLine from Win32_Process where CommandLine LIKE '%C:\\Windows\\system32\\notepad.exe%'"

完美运行

__GENUS          : 2
__CLASS          : Win32_Process
__SUPERCLASS     :
__DYNASTY        :
__RELPATH        :
__PROPERTY_COUNT : 1
__DERIVATION     : {}
__SERVER         :
__NAMESPACE      :
__PATH           :
CommandLine      : "C:\Windows\system32\notepad.exe"

也许PowerShell和WMI之间的通配符有问题,但任何人都可以帮助我使过滤器CommandLine='C:\Windows\system32\notepad.exe'工作

4

3 回答 3

1

CommandLine 属性的值包含引号,因此它们也需要转义。

一个有效但可怕的字符串是:

gwmi -Query "Select * from Win32_Process where CommandLine = '`"c:\\windows\\system32\\notepad.exe`"'"
于 2011-10-06T11:44:50.463 回答
0

您需要包含引号,但由于我不记得如何在 WQL 中转义它们,所以我会在 PSH 中进行:

gwmi -class Win32_Process -filter "CommandLine like '`"C:\\Windows\\system32\\notepad.exe`"'"

过滤器表达式用双引号括起来,字符串参数LIKE用单引号括起来。作为该参数一部分的双引号需要从 PowerShell 中引用。

于 2011-10-06T11:59:41.633 回答
0
Get-Process | ? {$_.Path -eq 'C:\Windows\system32\notepad.exe'}

Get-Process | ? {$_.processname -eq 'notepad'}
于 2011-10-12T10:21:08.210 回答