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.