Created
August 26, 2015 14:44
-
-
Save lazywinadmin/e7350ee7df6bc30f2ce5 to your computer and use it in GitHub Desktop.
Timeout in powershell
This file contains hidden or 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
#requires -Version 2 | |
$maximumRuntimeSeconds = 3 | |
$process = Start-Process -FilePath powershell.exe -ArgumentList '-Command Start-Sleep -Seconds 4' -PassThru | |
try | |
{ | |
$process | Wait-Process -Timeout $maximumRuntimeSeconds -ErrorAction Stop | |
Write-Warning -Message 'Process successfully completed within timeout.' | |
} | |
catch | |
{ | |
Write-Warning -Message 'Process exceeded timeout, will be killed now.' | |
$process | Stop-Process -Force | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment