2015-05-06 19 views
0

我在寫一個vb腳本來監視一個進程。該腳本監視過程的狀態,如果該進程沒有因爲10分鐘運行它應該執行一個command.Below是我的腳本:需要監視一個進程

set objWMIService = GetObject ("winmgmts:") 
    foundProc = False 
    procName = "calc.exe" 

    Dim wshell 

    ' Initialise the shell object to return the value to the monitor 
    Set wshell = CreateObject("WScript.Shell") 
    if err.number <> 0 then 
        WScript.Echo "Error: could not create WScript.Shell (error " & err.number & ", " & err.Description & ")" 
        WScript.quit(255) 
    end if 

    for each Process in objWMIService.InstancesOf ("Win32_Process") 
     If StrComp(Process.Name,procName,vbTextCompare) = 0 then 
      foundProc = True 
      procID = Process.ProcessId 
     End If 
    Next 

#####code to check the proces status 

    If foundProc = True Then 
     WScript.Quit(0) 


    Else 

     WScript.sleep(1*60*1000) 
     If foundProc = True Then 
     WScript.Echo "Found Process (" & procID & ")" 
     Else 
      WScript.Echo "Process not running since 10 mins" 
      WScript.Quit(0) 

     End If 

    End If 
+0

我有查詢,如果我執行腳本,如果假設進程停止一分鐘腳本eexcutes後。下一次迭代將在10分鐘後檢查狀態。所以9分鐘的差距是巨大的。我需要連續監視它,並且應該在10分鐘後開始執行命令。 – rahnik67

回答

0
Set WshShell = WScript.CreateObject("WScript.Shell") 
Set objWMIService = GetObject("winmgmts:\\.\root\CIMV2") 
Set objEvents = objWMIService.ExecNotificationQuery _ 
    ("SELECT * FROM Win32_ProcessTrace") 

Do 

    Set objReceivedEvent = objEvents.NextEvent 
    If objReceivedEvent.ProcessName = "svchost.exe" then msgbox objReceivedEvent.ProcessName 
' msgbox objReceivedEvent.ProcessName 
Loop 

你也有Win32_ProcessTraceStartWin32_ProcessTraceStop。以上代碼都是。

在WScript.Shell上進行錯誤檢查也是毫無意義的。這是一個系統組件 - 它應該可用。此外,如果它不是你的腳本不會運行,因爲wscript不可用來運行你的腳本。