Powershell - 通过多个名称过滤WMIObject进程

Cha*_*tte 1 powershell wmi

我正在尝试获取正在运行的进程列表并按两个进程名称进行筛选 - 任何人都可以告诉我如何使其正常工作?

到目前为止,我已经开始工作并过滤掉一个进程名称:

$rn = Get-WMIObject Win32_Process -computer servername `
  -credential mydomain\administrator -filter "Name='program1.exe'" |
  select -expand path
$lst = Get-Content “C:\path\path2\List.txt”
Compare-Object $lst $rn
Run Code Online (Sandbox Code Playgroud)

我想要它做的是过滤两个进程名称,但我尝试过的任何东西都没有.有任何想法吗?

Nic*_*Nic 5

以下是如何获取一组完整的Process对象,这些对象与您感兴趣的进程名称列表相匹配.

$ProcessNames = @( 'explorer.exe', 'notepad.exe' )

Get-WmiObject Win32_Process -Computer 'localhost' |
  Where-Object { $ProcessNames -contains $_.Name } | 
  Select-Object ProcessID, Name, Path |
  Format-Table -AutoSize
Run Code Online (Sandbox Code Playgroud)

此示例查找所有进程,然后通过将它们发送到管道筛选器来筛选该列表,该筛选器检查进程名称是否包含在有趣进程名称列表中.以这种方式使用管道的主要好处是您可以轻松访问返回进程的其他属性(例如ProcessID).

ProcessID Name         Path
--------- ----         ----
     5832 explorer.exe C:\Windows\Explorer.EXE
     4332 notepad.exe  C:\Windows\system32\NOTEPAD.EXE
     2732 notepad.exe  C:\Windows\system32\notepad.exe
Run Code Online (Sandbox Code Playgroud)