jcunews

ChkProcRun.vbs

Mar 9th, 2019 (edited)
689
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'VBScript to check how long a process has been running, optionally check
  2. 'whether it has been running for a specified amount of time, and optionally
  3. 'wait until it is run for a specified amount of time, and optionally then
  4. 'kills it (may require Administrator rights).
  5. '
  6. 'Display process run time in seconds e.g.:
  7. '  cscript chkprocrun.vbs "explorer.exe"
  8. '
  9. 'Check process ID 1234 for specific run time of 100 seconds e.g.:
  10. '  cscript chkprocrun.vbs 1234 100
  11. '
  12. 'Wait until process run time is 15 minutes e.g.:
  13. '  cscript chkprocrun.vbs process.exe 15m /wait
  14. '
  15. 'Wait until process run time is 12 hours then kill it e.g.:
  16. '  cscript chkprocrun.vbs process.exe 12h /waitkill
  17. '
  18. 'https://www.reddit.com/user/jcunews1/
  19.  
  20. sub help
  21.   wscript.echo _
  22.     "ChkProcRun {process name/id} [duration[m|h]] [/wait|/waitkill]" _
  23.     & vbcrlf & vbcrlf & "Duration is in seconds. Or minutes/hours if it has " _
  24.     & """m""/""h"" suffix." _
  25.     & vbcrlf & "/wait switch waits until process is running for the " _
  26.     & "specified amount of time." _
  27.     & vbcrlf & "/waitkill switch is like /wait but then kill the process." _
  28.     & vbcrlf & vbcrlf & "Exit codes:" _
  29.     & vbcrlf & "0 = Process has been running for at least the specified " _
  30.     & "amount of time." _
  31.     & vbcrlf & "1 = Process has not been running for at least the specified " _
  32.     & "amount of time." _
  33.     & vbcrlf & "2 = Process is not found or has been terminated." _
  34.     & vbcrlf & "3 = One or more parameters are invalid or absent."
  35.   wscript.quit 3
  36. end sub
  37.  
  38. if wscript.arguments.count < 1 then help
  39. pn = wscript.arguments(0)
  40. if isnumeric(pn) then
  41.   if pn <= 0 then help
  42.   pn = pn * 1
  43.   pid = int(pn)
  44.   if pid <> pn then help
  45.   fd = "processid"
  46. else
  47.   fd = "name"
  48. end if
  49.  
  50. if wscript.arguments.count >= 2 then
  51.   i = wscript.arguments(1)
  52.   mul = ucase(right(i, 1))
  53.   if mul = "M" then
  54.     mul = 60
  55.     i = left(i, len(i) - 1)
  56.   elseif mul = "H" then
  57.     mul = 3600
  58.     i = left(i, len(i) - 1)
  59.   else
  60.     mul = 1
  61.   end if
  62.   if not isnumeric(i) then help
  63.   if i < 0 then help
  64.   i = i * 1
  65.   dur = int(i)
  66.   if dur <> i then help
  67.   dur = dur * mul
  68.  
  69.   if wscript.arguments.count >= 3 then
  70.     wait = ucase(wscript.arguments(2)) = "/WAIT"
  71.     waitkill = ucase(wscript.arguments(2)) = "/WAITKILL"
  72.   else
  73.     wait = false
  74.     waitkill = false
  75.   end if
  76. else
  77.   dur = 0
  78.   wait = false
  79.   waitkill = false
  80. end if
  81.  
  82. set wmi = getobject("winmgmts:\\.\root\cimv2")
  83. set cols = wmi.execquery("select * from win32_process where " & fd & "='" & _
  84.   pn & "'", , 48)
  85. res = 2
  86. for each itm in cols
  87.   cd = itm.creationdate
  88.   dt = dateserial(left(cd, 4), mid(cd, 5, 2), mid(cd, 7, 2)) + _
  89.     timeserial(mid(cd, 9, 2), mid(cd, 11, 2), mid(cd, 13, 2))
  90.   dif = int((now - dt) * 86400)
  91.   if not (wait or waitkill) then wscript.echo dif
  92.   if dif >= dur then
  93.     res = 0
  94.   else
  95.     res = 1
  96.     if wait or waitkill then wscript.sleep (dur - dif) * 1000
  97.   end if
  98. next
  99. if (res <> 2) and (wait or waitkill) then
  100.   res = 2
  101.   set cols = wmi.execquery("select * from win32_process where " & fd & "='" & _
  102.     pn & "'", , 48)
  103.   for each itm in cols
  104.     cd = itm.creationdate
  105.     dt = dateserial(left(cd, 4), mid(cd, 5, 2), mid(cd, 7, 2)) + _
  106.       timeserial(mid(cd, 9, 2), mid(cd, 11, 2), mid(cd, 13, 2))
  107.     dif = int((now - dt) * 86400)
  108.     wscript.echo dif
  109.     if dif >= dur then
  110.       res = 0
  111.     else
  112.       res = 1
  113.     end if
  114.     if waitkill then
  115.       set sh = createobject("wscript.shell")
  116.       if fd = "name" then
  117.         sh.run "taskkill /f /im """ & pn & """", 0, true
  118.       else
  119.         sh.run "taskkill /f /pid " & pid, 0, true
  120.       end if
  121.     end if
  122.   next
  123. end if
  124. wscript.quit res
Add Comment
Please, Sign In to add comment