Skip to content

Instantly share code, notes, and snippets.

@yasinai
Forked from lazywinadmin/Timeout.ps1
Created January 14, 2018 13:57
Show Gist options
  • Save yasinai/a06539cae19f352a05736c83b9ed11d8 to your computer and use it in GitHub Desktop.
Save yasinai/a06539cae19f352a05736c83b9ed11d8 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