0

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.

  • `Invoke-Command -ScriptBlock {timeout.exe 10 /NOBREAK}` – 4c74356b41 Dec 13 '16 at 13:58
  • I mean, `Start-Job -ScriptBlock {Invoke-Command -ScriptBlock {timeout.exe 10 /NOBREAK}}` – 4c74356b41 Dec 13 '16 at 14:05
  • you can use Invoke-Command as job so it will run as a Background Job and you can receive it as well using _Receive-Job_ as like this: `Invoke-Command -ScriptBlock {timeout.exe 10 /NOBREAK} -AsJob` – Ranadip Dutta Dec 13 '16 at 14:32

1 Answers1

1

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
campbell.rw
  • 1,366
  • 12
  • 22