我是一个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"}
但很明显,那会显示所有的进程,有什么办法可以把所有的进程都删掉,但只列出运行上述进程的电脑?
先谢谢你。
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
如果没有找到进程,则会抛出一个错误。