Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'VBScript to check how long a process has been running, optionally check
- 'whether it has been running for a specified amount of time, and optionally
- 'wait until it is run for a specified amount of time, and optionally then
- 'kills it (may require Administrator rights).
- '
- 'Display process run time in seconds e.g.:
- ' cscript chkprocrun.vbs "explorer.exe"
- '
- 'Check process ID 1234 for specific run time of 100 seconds e.g.:
- ' cscript chkprocrun.vbs 1234 100
- '
- 'Wait until process run time is 15 minutes e.g.:
- ' cscript chkprocrun.vbs process.exe 15m /wait
- '
- 'Wait until process run time is 12 hours then kill it e.g.:
- ' cscript chkprocrun.vbs process.exe 12h /waitkill
- '
- 'https://www.reddit.com/user/jcunews1/
- sub help
- wscript.echo _
- "ChkProcRun {process name/id} [duration[m|h]] [/wait|/waitkill]" _
- & vbcrlf & vbcrlf & "Duration is in seconds. Or minutes/hours if it has " _
- & """m""/""h"" suffix." _
- & vbcrlf & "/wait switch waits until process is running for the " _
- & "specified amount of time." _
- & vbcrlf & "/waitkill switch is like /wait but then kill the process." _
- & vbcrlf & vbcrlf & "Exit codes:" _
- & vbcrlf & "0 = Process has been running for at least the specified " _
- & "amount of time." _
- & vbcrlf & "1 = Process has not been running for at least the specified " _
- & "amount of time." _
- & vbcrlf & "2 = Process is not found or has been terminated." _
- & vbcrlf & "3 = One or more parameters are invalid or absent."
- wscript.quit 3
- end sub
- if wscript.arguments.count < 1 then help
- pn = wscript.arguments(0)
- if isnumeric(pn) then
- if pn <= 0 then help
- pn = pn * 1
- pid = int(pn)
- if pid <> pn then help
- fd = "processid"
- else
- fd = "name"
- end if
- if wscript.arguments.count >= 2 then
- i = wscript.arguments(1)
- mul = ucase(right(i, 1))
- if mul = "M" then
- mul = 60
- i = left(i, len(i) - 1)
- elseif mul = "H" then
- mul = 3600
- i = left(i, len(i) - 1)
- else
- mul = 1
- end if
- if not isnumeric(i) then help
- if i < 0 then help
- i = i * 1
- dur = int(i)
- if dur <> i then help
- dur = dur * mul
- if wscript.arguments.count >= 3 then
- wait = ucase(wscript.arguments(2)) = "/WAIT"
- waitkill = ucase(wscript.arguments(2)) = "/WAITKILL"
- else
- wait = false
- waitkill = false
- end if
- else
- dur = 0
- wait = false
- waitkill = false
- end if
- set wmi = getobject("winmgmts:\\.\root\cimv2")
- set cols = wmi.execquery("select * from win32_process where " & fd & "='" & _
- pn & "'", , 48)
- res = 2
- for each itm in cols
- cd = itm.creationdate
- dt = dateserial(left(cd, 4), mid(cd, 5, 2), mid(cd, 7, 2)) + _
- timeserial(mid(cd, 9, 2), mid(cd, 11, 2), mid(cd, 13, 2))
- dif = int((now - dt) * 86400)
- if not (wait or waitkill) then wscript.echo dif
- if dif >= dur then
- res = 0
- else
- res = 1
- if wait or waitkill then wscript.sleep (dur - dif) * 1000
- end if
- next
- if (res <> 2) and (wait or waitkill) then
- res = 2
- set cols = wmi.execquery("select * from win32_process where " & fd & "='" & _
- pn & "'", , 48)
- for each itm in cols
- cd = itm.creationdate
- dt = dateserial(left(cd, 4), mid(cd, 5, 2), mid(cd, 7, 2)) + _
- timeserial(mid(cd, 9, 2), mid(cd, 11, 2), mid(cd, 13, 2))
- dif = int((now - dt) * 86400)
- wscript.echo dif
- if dif >= dur then
- res = 0
- else
- res = 1
- end if
- if waitkill then
- set sh = createobject("wscript.shell")
- if fd = "name" then
- sh.run "taskkill /f /im """ & pn & """", 0, true
- else
- sh.run "taskkill /f /pid " & pid, 0, true
- end if
- end if
- next
- end if
- wscript.quit res
Add Comment
Please, Sign In to add comment