Calling multiple URLs at a time using multithreading in powershell Calling multiple URLs at a time using multithreading in powershell powershell powershell

Calling multiple URLs at a time using multithreading in powershell


You will run into a couple of issues with the code above. The scriptblock gets transferred to a different PowerShell.exe process to execute so it won't have acess to $queries. You will to pass that it like so:

...$scriptblock = {param($queries)    ...}...$jobs += Start-Job $scriptblock -Arg $queries

The other issue is that you never remove a completed job from $job so once this $jobs.Count -lt 5 expression evals to false because the count has reached 5, you'll never add anymore jobs. Try something like this:

$jobs | Wait-Job -Any$jobs = $jobs | Where ($_.State -eq 'Running'}

Then you'll wind up with only the running jobs in $jobs which will allow you to start more jobs as previous jobs complete (or fail).