0

下面是我一直在尝试组合的脚本,以检查进程是否在远程计算机列表上运行。它有效,因为它报告进程正在远程机器上运行,但如果它没有运行,我没有得到任何指示,它只是跳过那台机器。如果进程未运行,我所有显示备用消息的尝试都失败了。有没有办法说如果没有找到PROCESS.exe,显示:

“进程未在” & ComputerName 上运行

然后继续循环遍历所有机器?谢谢您的帮助。

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
4

2 回答 2

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
于 2013-07-01T15:49:46.033 回答
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
于 2013-07-01T15:34:46.200 回答