我正在尝试获取正在运行的进程的列表,并按两个进程名进行过滤--谁能告诉我如何让它工作?
到目前为止,我已经让它工作了,并过滤掉了一个进程名称:
$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
我想要它做的是过滤两个进程名,但我尝试过的都不起作用。有什么想法吗?
发布于 2011-09-22 18:29:56
创建一个你想要的进程数组:
$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
}
我已经注释掉了你的比较,所以你可以清楚地看到我们是如何在数组中循环的。
发布于 2013-03-29 14:04:55
下面是如何获取与您感兴趣的进程名称列表相匹配的一组完整的进程对象。
$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
发布于 2011-09-22 18:26:55
使用WQL运算符,如OR、AND等:
Get-WMIObject Win32_Process -computer servername -credential mydomain\administrator -filter "Name='program1.exe' OR Name='program2.exe'"
https://stackoverflow.com/questions/7512734
复制相似问题