Powershell - 如何从列表中显示哪些计算机正在运行特定进程?

问题描述 投票:0回答:1

我是一个Powershell初学者,在一个使用Powershell 4.0的Win7环境中工作,所以无法导入任何模块或做任何复杂的事情,但我想知道如何才能生成一个txt文件,显示网络上正在运行特定进程的计算机,例如wusa.exe的Windows更新?

我已经有了一个包含所有计算机名称的txt文件列表,到目前为止有了这个文件。

$computers = gc "C:\PCList.txt"
foreach ($computer in $computers) {Get-process | out-file -Path "C:\TheseAreRunningWusa.txt"}

但很明显,那会显示所有的进程,有什么办法可以把所有的进程都删掉,但只列出运行上述进程的电脑?

先谢谢你。

windows powershell windows-7 powershell-4.0
1个回答
0
投票

Get-Process命令允许你指定一个远程计算机运行,以及你要寻找的服务。

$computers = Get-Content "C:\PCList.txt"
$output = @()

foreach ($computer in $computers) {
  if(Get-Process "myProcessName" -ComputerName $computer -ErrorAction SilentlyContinue) {
    $output += $computer
  }
}
$output | Set-Content "C:\TheseAreRunningMyProcess.txt"

注意:我使用了 -ErrorAction SilentlyContinue 作为 Get-Process 如果没有找到进程,则会抛出一个错误。

© www.soinside.com 2019 - 2024. All rights reserved.