title: Powershell start-job, wait-job, host thread never exits when run from ASP.NET IIS
body: <p>I am currently trying to build a threaded cleanup script with powershell, initiated from an IIS.
  I have made a threaded "Kill process by owner" using powershell remoting, running from the same list of servers as my cleanup script, and that works no problem.</p>
  
  <pre><code>$jobs = @()
  foreach ($comp in $comps) {
      $jobs += start-job -FilePath ("CleanupJob.ps1") -ArgumentList $comp, $username
      Write-Host "Started Job on: $comp"
  }
  foreach($job in $jobs)
  {
      $job | wait-job | out-null
      receive-job $job.ID
  }
  
  Remove-Job -State Completed 
  </code></pre>
  
  <p>When i run my script from the powershell console, the whole thing runs perfectly, main thread starts 28 new processes, which start a remoting connection to each server, and waits for all the jobs to finish. When they are finished, i get my output and the host thread exists. All running as planned.</p>
  
  <p>Not so when I run it from my asp.net application, i get ""Started Job on: $comp"" for each of my 28 servers, but only a result from the first 14, and then the host thread just sits there. (untill i kill it with fire, and my output is returned to the asp.net webpage)</p>
  
  <p>I have no way to see what happens in the script when i run it from the asp.net page.All i can see is cpu/ram usage drop to the same levels as when i run it from the PSconsole, but my main thread never closes. So I do believe the script works as supposed, but my webpage hangs untill the main thread closes(which it never does, as stated).</p>
  
  <hr>
  
  <p>This is how I call my script (not the pretty .net &lt;3 powershell way:)</p>
  
  <pre><code>public string RunProgramme(string scriptpath, string arguments)
  {
      ProcessStartInfo startInfo = new ProcessStartInfo();
      startInfo.FileName = @"powershell.exe";
      startInfo.Arguments = "&amp; \'"+scriptpath+"\' "+ arguments;
  
      //return startInfo.Arguments;
      startInfo.RedirectStandardOutput = true;
      startInfo.RedirectStandardError = true;
      startInfo.UseShellExecute = false;
      startInfo.CreateNoWindow = true;
      Process process = new Process();
      process.StartInfo = startInfo;
      process.Start();
  
      string output = process.StandardOutput.ReadToEnd();
      string errors = process.StandardError.ReadToEnd();
      return output;
  }
  </code></pre>
  
  <p>The mystery deepens, added this line to my threaded jobs</p>
  
  <pre><code>Invoke-Command -Session $session  -ScriptBlock {param($path) net send mymachine $path}  -Args $msg 
  </code></pre>
  
  <p>And when i run my script from the IIS, i receive message from each machine. 
  Just as my ram usage shows, all the jobs are run, but the output isnt returned properly, and my host thread just sits there...waiting...</p>