Skip to content

Instantly share code, notes, and snippets.

@lazywinadmin
Created August 26, 2015 14:44
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save lazywinadmin/e7350ee7df6bc30f2ce5 to your computer and use it in GitHub Desktop.
Save lazywinadmin/e7350ee7df6bc30f2ce5 to your computer and use it in GitHub Desktop.
Timeout in powershell
#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