title: How to have multiple return values returned together within Powershell runspace threads?
body: <p>How can I have the second <code>$scriptBlock</code> work as the first <code>$scriptBlock</code> does? The differences between the 2 are that the first one returns a single object from a command and the second one should return an array of objects from multiple commands?</p>
  
  <p>To test this code you only need fill in the server names for <code>$servers</code> and an Exchange powershell hyperlink for <code>$connectionUri</code> and alternate between commenting out the first <code>$scriptBlock</code> (working) and the second <code>$scriptBlock</code> (not working).</p>
  
  <p>Thanks</p>
  
  <pre><code>cls
  
  Write-Host "Aynchronous";
  
  # Create session state
  $stopWatch = [System.Diagnostics.Stopwatch]::StartNew();
  
  $myString = "this is session state!";
  $sessionState = [System.Management.Automation.Runspaces.InitialSessionState]::CreateDefault();
  $sessionstate.Variables.Add((New-Object -TypeName System.Management.Automation.Runspaces.SessionStateVariableEntry -ArgumentList "myString" ,$myString, "example string"));
  
  # Create runspace pool consisting of $numThreads runspaces
  $minimumAmountOfThreads = 1;
  $maximumAmountOfThreads= 15;
  $RunspacePool = [RunspaceFactory]::CreateRunspacePool(1, 15, $sessionState, $Host);
  $RunspacePool.Open();
  
  $servers = @("server1","server2","server3");
  $connectionUri = "http://[something]/powershell";
  
  $threads = @();
  $Jobs = @();
  $asynchronousThreadCount = 0;
  foreach ($server in $servers)
  {
      $scriptBlock = "import-pssession (new-pssession -ConfigurationName Microsoft.Exchange -ConnectionUri $connectionUri) -AllowClobber; get-exchangeserver $server;"; # working
      #$scriptBlock = "import-pssession (new-pssession -ConfigurationName Microsoft.Exchange -ConnectionUri $connectionUri) -AllowClobber; $returnValue = @(); $returnValue += Get-MailboxDatabase -Server $server; return $returnValue;"; # NOT working
          #$scriptBlock = "import-module 'activedirectory'; Get-ADDomain"; # working
      #$scriptBlock = "import-module 'activedirectory'; $returnValue = @(); $returnValue += Get-ADDomain; return $returnValue;" # NOT working
  
      $asynchronousThreadCount ++;
      $runspaceObject = [PSCustomObject] @{
          Runspace = [PowerShell]::Create()
          Invoker = $null
      }
      $runspaceObject.Runspace.RunSpacePool = $runspacePool;
      $runspaceObject.Runspace.AddScript($scriptBlock) | Out-Null;
      $runspaceObject.Runspace.AddArgument($c) | Out-Null;
      $runspaceObject.Invoker = $runspaceObject.Runspace.BeginInvoke();
      $threads += $runspaceObject;
      $elapsed = $StopWatch.Elapsed;
  
      Write-Host "A synchronous created thread $asynchronousThreadCount " $elapsed;
  }
  
  Write-Host $threads.Count;
  Write-Host "";
  Write-Host "Waiting.." -NoNewline;
  Do {
     Write-Host "." -NoNewline;
     Start-Sleep -Seconds 1;
  } While ( $runspaceObject.Invoker.IsCompleted -contains $false ); 
  
  $resultsAsynchronous = @();
  
  foreach ($tr in $threads)
  {
      $resultsAsynchronous += $tr.Runspace.EndInvoke($tr.Invoker);
      $tr.Runspace.Dispose();
  }
  $procCountMultiThread = $resultsAsynchronous.Count;
  
  $runspacePool.Close();
  $runspacePool.Dispose();
  
  $elapsed = $StopWatch.Elapsed;
  Write-Host "Multithread elapsed time: $elapsed";
  Write-Host "Asynchronous return value count " $resultsAsynchronous.Count;
  $resultsAsynchronous #| fl fqdn, AdminDisplayVersion;
  </code></pre>