1

I have multiple different scriptblocks that I want to run with Start-RSJob from the PoshRSJob module.

I have the following code:

$SB1 = {Write-Output "Hello from 1"}
$SB2 = {Write-Output "Hello from 2"}
$SB3 = {Write-Output "Hello from 3"}
$SB4 = {Write-Output "Hello from 4"}

$SBs = @($SB1, $SB2, $SB3, $SB4)

$SBs | Start-RSJob -ScriptBlock {
    $PSBoundParameters.GetEnumerator() | ForEach {
        . $_.Value
    }
}

Get-RSJob | Wait-RSJob

which works, but I'm getting sometimes (sometimes no errors, sometimes with erros) errors inside the jobs like for the job that executed $SB3:

Stack empty.
Hello from 3 

And from the job that executed $SB4:

Stack empty.
Hello from 4

And from the other two jobs I got the expected output:

Hello from 1
Hello from 2

I'm not using the ForEach to start new jobs because that is what the creator said:

Notice that I do not use ForEach when using Start-RSJob. This takes input directly from the pipeline to work properly and fully utilize the throttling capabilities.

Edit:

So the following works:

$SB1 = {Write-Output "Hello from 1"}
$SB2 = {Write-Output "Hello from 2"}
$SB3 = {Write-Output "Hello from 3"}
$SB4 = {Write-Output "Hello from 4"}

$SBs = @($SB1, $SB2, $SB3, $SB4)

Get-RSJob | Remove-RSJob

$SBs |Start-RSJob -ScriptBlock {
    $PSBoundParameters.GetEnumerator() | ForEach {
        $SB_run = [ScriptBlock]::Create($_.Value)
        $SB_run.Invoke()
    }
}

Get-RSJob | Wait-RSJob | Receive-RSJob

But I have no idea why the previous one didn't.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
sheldonzy
  • 5,505
  • 9
  • 48
  • 86
  • You might get some clue from https://stackoverflow.com/questions/53470066/powershell-start-job-invoke-function-argument-with-parameter/53474503#53474503 – Prasoon Karunan V Dec 02 '18 at 14:45

1 Answers1

0

from what i can tell, you need to enclose the call to the RSJob cmdlet in a ForEach-Object call. it seems that Start-RSJob does not directly accept a scriptblock via the pipeline. the Start-Job cmdlet has that limitation.

Lee_Dailey
  • 7,292
  • 2
  • 22
  • 26