title: PowerShell process handling - Start multiple SqlPackage.exe processes in a PowerShell loop
body: <p>Is there a way to start worker processes of SqlPackage.exe where the target databases are stored in an array such that you can loop over it (all other parameters are the same)?</p>
  
  <p>I would like to suppress all prints from these processes UNLESS there's an error, and then show a report at the end. How would I do this?</p>
  
  <p>Right now I have a loop like the following (as a start):</p>
  
  <pre><code>$arguments = @(
      "/Action:Import",
      "/SourceFile:$($bacpacSource)",
      "/TargetServerName:$($serverName)"
  )
  
  for ($i = 1; $i -lt 4; $i++) {
      $targetDatabase = $baseDatabaseName + $i
      $arguments += "/TargetDatabaseName:$($targetDatabase)"
      Start-Process -FilePath $sqlPackagePath $arguments -NoNewWindow -PassThru
  }
  </code></pre>
  
  <p>I found the <code>Start-Process</code> somewhere and I am sure that I need to store the object returned by this function and use it somehow. I would like to start all the processes and then wait for all of them to finish, at which point I will investigate whether or not all succeeded. If there are failures I want to print the error information from that process. Right now all processes print at the same time and the whole buffer is corrupted after they all finish.</p>
  
  <p>Is this possible in PowerShell?</p>
  
  <p>PS: The script is meant to deploy updates using .dacpac files to production later, but this question is strictly related to the handling of Windows processes and errors.</p>
  
  <p>EDIT: I will probably need a way to create and maintain a maximum amount of processes at once, and exchange when some are done. So let's say that at any moment I may only have 10 SqlPackage.exe processes connecting to the server. How would I create such a <code>barrier lock</code> (think that's what it's called) in PowerShell?</p>