I'm trying to run two Invoke-Sqlcmd in parallel and then wait all the results.
$server1 = '...'
$server2 = '...'
workflow work {
parallel {
$r1 = Invoke-Sqlcmd -ServerInstance $server1 'select 1 a'
$r2 = Invoke-Sqlcmd -ServerInstance $server2 'select 2 a'
}
}
work
# do something with $r1 and $r2. How to access the variables here?
- How to pass
$server1and$server2to the code block? - How to get
$r1and$r2from the parallel block?
Start-ThreadJob- see this answer.param (). Not sure how to get the results except$result = work $server1 $server2. Maybe jobs is easier.Invoke-SqlCmd, as detailed in my comment on your linked question. You can work around the problem with regular child-process-based background jobs (Start-Job), but note that they have a lot more overhead than thread jobs - for long-running SQL queries that won't matter, however.