How to get values from workflow parallel code block?

300 Views Asked by At

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?
  1. How to pass $server1 and $server2 to the code block?
  2. How to get $r1 and $r2 from the parallel block?
1

There are 1 best solutions below

2
js2010 On

So pass in servers with param, and return a hashtable of both results.

$server1 = '...'
$server2 = '...'

workflow work {
  param ($server1, $server2)
  parallel {
    @{r1 = Invoke-Sqlcmd -ServerInstance $server1 'select 1 a'}
    @{r2 = Invoke-Sqlcmd -ServerInstance $server2 'select 2 a'}
  }
}
$r = work $server1 $server2
$r.r1
$r.r2