I have a PS1 file with Invoke-RestMethod on 10 different urls. I want to make them execute in parallel and get the data from them. All the examples I've seen are for running scripts in parallel.
How can I execute:
$dataA = Invoke-RestMethod -Uri 'https://xyz/A' -WebSession $WebSession -Method Post -Body $queryA
.
.
.
$dataK = Invoke-RestMethod -Uri 'https://xyz/K' -WebSession $WebSession -Method Post -Body $queryK
The above code in parallel, such that I could use the data in dataA to dataK outside the parallel block.
Thanks
2 Answers 2
You can use Powershell Workflow.
workflow paralleltest {
parallel {
$dataA = Invoke-RestMethod -Uri 'https://xyz/A' -WebSession $WebSession -Method Post -Body $queryA
$dataK = Invoke-RestMethod -Uri 'https://xyz/K' -WebSession $WebSession -Method Post -Body $queryK
}
}
Comments
You could use the cmdlet "ForEach-Object" with the parameter "-Parallel" explained here: PowerShell ForEach-Object Parallel Feature
Although, I havent done it before, I would suggest the following:
- Define your variables $dataA..$dataZ
2.$dataA..$dataZ|ForEach-Object...-parallel