我正在使用psexec在网络上的所有PC上从cmd自动运行,以检查某个进程是否正在运行。但是我想要一个列表,上面有运行该服务的所有pc名称。我如何从powershell中做到这一点?
这就是我现在要跑的。2个批处理文件和1个文本文件。
get.bat
tasklist | findstr-pmill.exe>>dc-01\c$\00001.txt
run_get.bat
psexec@%1-u管理员-p密码-c"c:\get.bat"
pclist.txt
我从中得到的结果只是所有的pmill.exe,我想知道我是否可以输出运行pmill.exe的PC名称?
提示plz!
如果所有计算机都安装了启用远程处理的powershell,则可以尝试下面的脚本。它还输出无法访问的计算机,因此如果您愿意,可以稍后重新测试它们。如果您不需要它,只需删除catch
-块(或所有try/catch
)内的内容:
$out = @()
Get-Content "pclist.txt" | foreach {
$pc = $_
try {
if((Get-Process -Name "pmill" -ComputerName $pc) -ne $null) {
$out += $_
}
} catch {
#Unknown error
$out += "ERROR: $pc was not checked. $_.Message"
}
}
$out | Set-Content "out.txt"
pclist.txt:
graimer-pc
pcwithoutprocesscalledpmill
testcomputer
testpc
graimer-pc
Out.txt(日志):
graimer-pc
ERROR: testcomputer is unreachable
ERROR: testpc is unreachable
graimer-pc
取决于可用的远程处理类型:
-
如果Windows远程管理(例如Services.msc可以连接),那么只需使用
Get-Service -Name theService -computer TheComputer
如果服务运行时包含有关该服务的信息(如其状态)或者如果没有安装则什么都没有,所以假设
pclist.txt
是每行一个计算机名称,获取运行服务的计算机的列表(在用正确的替换serviceName
之后名称:这可能与进程名称不同):Get-Content pclist.txt | Where-Object { $s = Get-Service -name 'serviceName' -computer $_ $s -or ($s.Status -eq Running) }
-
如果WMI可用,请使用上面的
Get-WmiObject win32_service -filter 'name="serviceName"' and the
状态member of the returned object in the
Where对象。 -
PowerShell远程处理:使用
Invoke-Command -ComputerName dev1 -ScriptBlock { Get-Service serviceName }
在远程计算机上运行Get-Service
以返回相同的对象(但使用PSComputerName
属性添加)