Skip to content

Instantly share code, notes, and snippets.

@mwulftange
Created February 9, 2016 09:46
Show Gist options
  • Save mwulftange/2f20a5d564f94074cc71 to your computer and use it in GitHub Desktop.
Save mwulftange/2f20a5d564f94074cc71 to your computer and use it in GitHub Desktop.
Exec with timeout in VBScript
' Calls WshShell.Exec with c and kills the process tree after the specified timeout t
' Returns the created WshScriptExec object
Function Exec(c, t)
Dim s, e : Set s = CreateObject("WScript.Shell") : Set e = s.Exec(c)
Do While e.Status = 0
Call s.Run("waitfor /t 1 OneSecond", 0, True)
t = t - 1
If 0 >= t Then
Call s.Run("taskkill /t /f /pid " & e.ProcessId, 0, True)
Exit Do
End If
Loop
Set Exec = e
End Function
@Jwalker107
Copy link

Agreed on the wscript.sleep comment. Spawning an external process should be avoided when possible, as it comes with its own overhead. In my environment we audit Process Creation, so this would add a new security event log entry every second until the process ends or timeout occurs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment