title: How to execute a PowerShell function several times in parallel?
body: <p>I'm not sure whether to call this a need for multi-threading, job-based, or async, but basically I have a Powershell script function that takes several parameters and I need to call it several times with different parameters and have these run in parallel.</p>
  
  <p>Currently, I call the function like this:</p>
  
  <pre><code>Execute "param1" "param2" "param3" "param4"
  </code></pre>
  
  <p>How can I call this multiple times without waiting for each call to Execute return to the caller?</p>
  
  <p>Currently I'm running v2.0 but I can update if necessary</p>
  
  <p><strong>EDIT:</strong> here's what I have so far, which doesn't work:</p>
  
  <pre><code>$cmd = {
      param($vmxFilePath,$machineName,$username,$password,$scriptTpath,$scriptFile,$uacDismissScript,$snapshotName)
      Execute $vmxFilePath $machineName $username $password $scriptTpath $scriptFile $uacDismissScript $snapshotName
  }
  
  Start-Job -ScriptBlock $cmd -ArgumentList $vmxFilePath, $machineName, $username $password, $scriptTpath, $scriptFile, $uacDismissScript, $snapshotName
  </code></pre>
  
  <p>I get an error:</p>
  
  <blockquote>
    <p>cannot convert 'system.object[]' to the type 'system.management.automation.scriptblock' required by parameter 'initializationscript'. specified method is not supported</p>
  </blockquote>
  
  <p><strong>EDIT2:</strong> I've modified my script but I still get the error mentioned above. Here's my mod:</p>
  
  <pre><code>$cmd = {
      param($vmxFilePath,$machineName,$username,$password,$scriptTpath,$scriptFile,$uacDismissScript,$snapshotName)
      Execute $vmxFilePath $machineName $username $password $scriptTpath $scriptFile $uacDismissScript $snapshotName
  }
  
  Start-Job -ScriptBlock $cmd -ArgumentList $vmxFilePath, $machineName, $username $password, $scriptTpath, $scriptFile, $uacDismissScript, $snapshotName
  </code></pre>