7

I'm trying to do basic background jobs in PowerShell 2.0, and I'm seeing different things with start-job and invoke-command -asjob.

If I do this:

start-job -scriptblock {get-process}

I get a job object, but the child job (which is created automatically by start-job) always has a JobStateInfo of "NotStarted".

this, however, works as expected:

invoke-command -scriptblock {get-process} -computer localhost -asjob

I've run the enable-psremoting....anything else I need to do to get background jobs working?

Mike Shepard
  • 17,466
  • 6
  • 51
  • 69

2 Answers2

3

The first example using start-job does not use HTTP for the call and instead uses an IPC channel with WinRM to run; it does not require administrative privileges this way. The second example with invoke-command does require admin rights (by default) and will connect via HTTP and WinRM.

To be honest, I would have expected the second one to fail for most people. If you run: Receive-Job against the ID of the start-job invocation, do you get any error messages?

-Oisin

x0n
  • 51,312
  • 7
  • 89
  • 111
  • No, it just doesn't return anything (which I'd expect given that the child job is still NotStarted). – Mike Shepard Nov 12 '09 at 23:42
  • BTW...it's good to know that they work differently internally. Still doesn't explain why start-job doesn't work, but at least I'm not insane. – Mike Shepard Nov 12 '09 at 23:43
0

To receive an updated JobStateInfo you'll need to use Get-Job and the job created by Start-Job. Though, if you're using this information to see when the job finishes, Wait-Job or Receive-Job -wait might be better suited to your needs.

Wait-Job simply waits until the job, or list of jobs, indicated is finished before moving on. Receive-Job -wait does the same thing, but it also gathers the results/output of the job.

Jeffrey Bosboom
  • 13,313
  • 16
  • 79
  • 92
Skip
  • 1