I mean there is timeout.exe
but I don't think that gives you quite the same functionality that you are looking for.
I am not aware of a timeout
equivalent for Windows. Following the suggestion in the linked answer PowerShell jobs would be a suggestion on how to replicate timeout
s behavior. I rolled a simple sample function
function timeout{
param(
[int]$Seconds,
[scriptblock]$Scriptblock,
[string[]]$Arguments
)
# Get a time stamp of before we run the job
$now = Get-Date
# Execute the scriptblock as a job
$theJob = Start-Job -Name Timeout -ScriptBlock $Scriptblock -ArgumentList $Arguments
while($theJob.State -eq "Running"){
# Display any output gathered so far.
$theJob | Receive-Job
# Check if we have exceeded the timeout.
if(((Get-Date) - $now).TotalSeconds -gt $Seconds){
Write-Warning "Task has exceeded it allotted running time of $Seconds second(s)."
Remove-Job -Job $theJob -Force
}
}
# Job has completed natually
$theJob | Remove-Job -ErrorAction SilentlyContinue
}
This starts a job and keeps checking for its output. So you should get near live updates of the running process. You do not have to use -ScriptBlock
and could opt for -Command
based jobs. I will show an example using the above function and a script block.
timeout 5 {param($e,$o)1..10|ForEach-Object{if($_%2){"$_`: $e"}else{"$_`: $o"};sleep -Seconds 1}} "OdD","eVeN"
This will print the numbers 1 to 10 as well as the numbers evenness. Between displaying a number there will be a pause of 1 second. If the timeout is reached a warning is displayed. In the above example all 10 number will not be displayed since the process was only allowed 5 seconds.
Function could use some touching up and likely there is someone out there that might have done this already. My take on it at least.