Search code examples
windowspowershellpowershell-5.0job-control

PowerShell won't start job


I have the following PowerShell 5 program:

$job = Start-Job -ScriptBlock {timeout.exe 10 /NOBREAK}
Wait-Job $job

When I run it, it terminates immediately with the following output:

Id     Name            PSJobTypeName   State         HasMoreData     Location             Command
--     ----            -------------   -----         -----------     --------             -------
1      Job1            BackgroundJob   Completed     True            localhost            timeout.exe 10 /NOBREAK

and timeout.exe does not appear in Task Manager.

Why does it not wait 10 seconds before terminating? When I invoke timeout.exe 10 /NOBREAK outside a job, it does wait 10 seconds, as does invoking Start-Sleep 10 in a job.

I am not looking for a different way to solve the same problem (sleeping), but specifically for an answer to why this program behaves the way it does.


Solution

  • The problem here is that for timeout.exe to catch a Ctrl+C, it redirects the input, which doesn't work in a background job. Obviously if all you want to do is sleep, then Start-Sleep would work.

    You can view the output of your job, and the error message by doing:

    Receive-Job 1