Created
February 9, 2016 09:46
-
-
Save mwulftange/2f20a5d564f94074cc71 to your computer and use it in GitHub Desktop.
Exec with timeout in VBScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
' 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.