' Tobias Dietrich, 21.01.2009 ' 'process_runtime.vbs v1.0 ' 'this script checks if a process is running and how long it is running 'if a certain runtime is exceeded you get a critical ' 'cscript process_runtime.vbs /p:calc.exe /c:10 On error resume next Dim objWMIService, objProcess, colProcesses, ProcessId Dim objTime, objSystem, objDivTime Set args = WScript.Arguments.Named If (not args.Exists("p")) or (not args.Exists("c")) or args.Exists("h") Then WScript.Echo WScript.Echo "Usage: process_runtime.vbs /p:STRING /c:INTEGER" WScript.Echo " /p: process name; e.g. calc.exe " WScript.Echo " /c: max. process runtime" WScript.Echo " /h: help" WScript.Echo WScript.Quit 3 End If ' get the process name If args.Exists("p") Then processName=args.Item("p") End If ' get the critical time value c=1*args.Item("c") Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" _ & "." & "\root\cimv2") ' get process start time Set colProcesses = objWMIService.ExecQuery( _ "Select * from Win32_Process " _ & "Where Name = '" & processName & "'") For Each objProcess in colProcesses CreationDate = WMIDateStringToDate(objProcess.CreationDate) Next ' get current time Set colItems = objWMIService.ExecQuery( _ "Select * from Win32_OperatingSystem ") For Each objItem In colItems LocalTime = WMIDateStringToDate(objItem.LocalDateTime) Next ' get difference MinutesOld = DateDiff("n", CreationDate, LocalTime) outCode=0 If colProcesses.Count = 0 Then outCode=0 outText=" - Process is not running" Else If c=>MinutesOld Then outCode=0 outText=" - Process: " & processName & " = " & MinutesOld & " Minutes" End If If c