I got my main.ps1 script with a loop calling a sub.ps1. Currently I wait until the end of sub.ps1 to run the next one.
I would like to do some parallel processing, only in the case that it takes too long. Some subtleties: I can't edit sub.ps1 and I set env variables in main.ps1 for sub.ps1.
So I came out with the this first idea :
- creating an intermedate shell to run sub.ps1 and raise an event at its end
- wait that event in main.ps1 with a timeout
but not quite convinced, so I tried with Start-job and Wait-job, but doesn't work.
It shows the job ending instantly, not minding the sleep in inter_shell.ps1 and continue the loop. Also I'm not sure if it share the env variable.
I can't much share the code so here is some aproximations
main.ps1
# set dummy env var
$env:var1 = "aze"
# wait at least that time before start new process
$timeout = 10
# simulate the n process stack to run
for($i = 7; $i -lt 14; $i++) {
Write-Host "start $i"
# run intermediate shell that will run sub.ps1
$jobName = "Inter_$i"
Get-Date
$a = Start-Job -Name $jobName -ScriptBlock {powershell .\inter_shell.ps1 -workTime $i}
Get-Date
$a | Wait-Job -Timeout $timeout
Get-Date
}
inter_shell.ps1
param([int] $workTime)
$env:var1
# Simulate sub.ps1 process
Start-Sleep -Seconds $workTime