下面是我一直试图整理的脚本,以检查进程是否在远程计算机列表上运行。它工作,因为它报告该进程正在远程计算机上运行,但如果它没有运行,我没有任何指示,它只是跳过该计算机。如果进程未运行,我尝试显示备用消息的所有尝试都失败了。有没有办法说如果找不到PROCESS.exe,请显示:
“过程未运行”&计算机名
然后继续循环遍历所有机器? 谢谢你的帮助。
Set fso = CreateObject("Scripting.FileSystemObject")
Set listFile = fso.OpenTextFile("C:\list.txt")
Do While Not listFile.AtEndOfStream
ComputerName = listFile.ReadLine()
Set Service = GetObject("winmgmts:\\" & ComputerName &"")
for each Process in Service.InstancesOf ("Win32_Process")
If Process.Name = "PROCESS.exe" then
wscript.echo "PROCESS running on " & ComputerName
End If
Next
Loop
答案 0 :(得分:2)
当您找到匹配的进程时,只需在内部循环中设置一个标志。这样,无论您是否在循环结束后找到进程,都可以打印消息。此外,您应该添加一些错误处理(如果您无法连接到服务器),并且可能只使用ExecQuery
过滤器来查找您正在查找的进程名称,这样您就不必检索所有服务器的所有进程名称。
processname = "PROCESS.EXE"
query = "SELECT * FROM Win32_Process WHERE Name='" & processname & "'"
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile("C:\list.txt")
Do Until f.AtEndOfStream
skipServer = False
server = f.ReadLine
On Error Resume Next
Set wmi = GetObject("winmgmts://" & server &"/root/cimv2")
If Err Then
WScript.Echo "Error " & Err.Number & " connecting to " & server ": " _
& Err.Description
On Error Goto 0
skipServer = True
End If
On Error Goto 0
If Not skipServer Then
found = "not "
For Each process in wmi.ExecQuery(query)
found = ""
Exit For
Next
WScript.Echo "PROCESS.EXE " & found & "running on " & server & "."
End If
Loop
f.Close
答案 1 :(得分:1)
你能不能简单地使用切换?
foundIt = false
for each Process in Service.InstancesOf ("Win32_Process")
If Process.Name = "PROCESS.exe" then
foundIt = true
exit for
End If
Next
if (foundIt) then
wscript.echo "PROCESS running on " & ComputerName
else
wscript.echo "PROCESS NOT running on " & ComputerName
end if