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

发布于 2024-12-05 15:48:18 字数 378 浏览 1 评论 0原文

我正在尝试获取正在运行的进程的列表并按两个进程名称进行过滤 - 谁能告诉我如何使其正常工作?

到目前为止,我已经可以正常工作并过滤掉一个进程名称:

$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

我想要它做的是过滤两个进程名称,但我尝试过的任何方法都不起作用。有什么想法吗?

I am trying to get a list of running processes and filter by two process names - can any one tell me how to get this working?

I've so far got it working and filtering out one process name:

$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

What I want it to do is filter two process names but nothing I've tried works. Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

巴黎夜雨 2024-12-12 15:48:18

使用 WQL 运算符,如 OR、AND、LIKE 等:

Get-WMIObject Win32_Process -computer servername -credential mydomain\administrator -filter "Name='program1.exe' OR Name='program2.exe'"

Use WQL operators like OR, AND, LIKE etc:

Get-WMIObject Win32_Process -computer servername -credential mydomain\administrator -filter "Name='program1.exe' OR Name='program2.exe'"
凡尘雨 2024-12-12 15:48:18

以下是如何获取与您感兴趣的进程名称列表相匹配的完整 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

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

Here's how to get a complete set of Process objects which match a list of process names you're interested in.

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

Get-WmiObject Win32_Process -Computer 'localhost' |
  Where-Object { $ProcessNames -contains $_.Name } | 
  Select-Object ProcessID, Name, Path |
  Format-Table -AutoSize

This example finds all processes, then filters that list by sending them to a pipeline filter that checks to see if the process name is contained in the list of interesting process names. The main benefit of using the pipeline this way is that you can easily access other attributes (such as ProcessID) of the returned processes.

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
黎夕旧梦 2024-12-12 15:48:18

创建您要处理的进程的数组:

$processes = @('winword.exe', 'notepad.exe', 'excel.exe') | `
    % {
       $rn = Get-WMIObject Win32_Process -computer servername -credential mydomain\admin -filter "Name='$_'" | select -expand path 
       #$lst = Get-Content “C:\path\path2\List.txt” 
       #Compare-Object $lst $rn
       write-host $rn
    }

我已经注释掉了您的比较,以便您可以清楚地看到我们如何循环遍历该数组。

Create an array of the processes you're after:

$processes = @('winword.exe', 'notepad.exe', 'excel.exe') | `
    % {
       $rn = Get-WMIObject Win32_Process -computer servername -credential mydomain\admin -filter "Name='$_'" | select -expand path 
       #$lst = Get-Content “C:\path\path2\List.txt” 
       #Compare-Object $lst $rn
       write-host $rn
    }

I've commented out your compare so you can see how we are looping through the array clearly.

掐死时间 2024-12-12 15:48:18

如果我理解得很好试试这个:

$rn = Get-WMIObject Win32_Process -computer servername -credential mydomain\administrator -filter "Name='program1.exe OR Name='program2.exe'"
Compare-Object $rn[0].path $rn[1].path # if there are only one instance for process with name program1.exe and program2.exe

if I understood well try this:

$rn = Get-WMIObject Win32_Process -computer servername -credential mydomain\administrator -filter "Name='program1.exe OR Name='program2.exe'"
Compare-Object $rn[0].path $rn[1].path # if there are only one instance for process with name program1.exe and program2.exe
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文