title: How can I get invoke command to return an output to a variable?
body: <p>I'm trying to loop through an array of servers and run this test path command as a background thread. It needs to be a thread because I will be monitoring it for a time out and killing the thread if it exceeds that time out. </p>
  
  <p>I cannot get a variable return value, nor do I know how I would have separate variables for multiple machines or if I'm passing the variables properly. In c# I would use a thread collection, but I can't seem to figure out the syntax for Powershell.</p>
  
  <pre><code>foreach ($server in $servers)
  {
      Write-Host $server;
  
      $scriptBlock = 
      {
          $returnVal = Test-Path "\\$server\c$";
          return $returnVal;
      }
      $remoteReturnVal = Invoke-Command -ComputerName [Environment]::MachineName -ScriptBlock { $script } –AsJob;
  }
  </code></pre>
  
  <p>Here is how I can do it in Powershell with c# (the below works)</p>
  
  <pre><code>$shareCheck = 
  @'
  
  using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Data;
  using System.Drawing;
  using System.IO;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using System.Windows.Forms;
  
  namespace powershellConsoleForm
  {
      public class shares
      {
          public List&lt;string&gt; shareExists(List&lt;string&gt; servers, int timeOut)
          {
              List&lt;string&gt; returnValue = new List&lt;string&gt;();
              foreach (string server in servers)
              {
                  string path = @"\\" + server + @"\c$";
                  Func&lt;bool&gt; func = () =&gt; Directory.Exists(path);
                  Task&lt;bool&gt; task = new Task&lt;bool&gt;(func);
                  task.Start();
                  if (task.Wait(timeOut))
                  {
                      //return task.Value;
                      Console.Write(" ");
                      Console.Write("success " + task.Result);
                      Console.Write(" ");
                      returnValue.Add(server + "|" + task.Result.ToString());
                  }
                  else
                  {
                      Console.Write("timeout");
                      returnValue.Add(server + "|timeout");
                  }
              }
              Console.Write("Done");
              return returnValue;
          }
      }
  }
  '@
  
  try
  {
      $shareChecker = New-Object powershellConsoleForm.shares;
  }
  catch
  {
      $assemblies = ("System", "System.Collections", "System.ComponentModel", "System.Data", "System.Drawing", "System.IO", "System.Linq", "System.Management.Automation", "System.Security", "System.Threading.Tasks", "System.Windows.Forms", "System.Threading", "System.Collections.Concurrent", "System.Security.Principal");
      Add-Type -TypeDefinition $shareCheck -ReferencedAssemblies $assemblies -Language CSharp
      $shareChecker = New-Object powershellConsoleForm.shares;
  }
  
  $servers = @("server1", "server2", "server3", "server4", "server5");
  $timeOut = 11000;
  
  [int] $counter = 0;
  do
  {
      $counter ++;
      Write-Host $counter;
      $shareAvailibilities = $shareChecker.shareExists($servers, $timeOut);
      foreach ($shareAvailibility in $shareAvailibilities)
      {
          Write-Host $shareAvailibility;
      }
  
      Write-Host " ";
      Write-Host " ";
  
      Sleep 5;
  }while ($true)
  </code></pre>