title: How to make C# Powershell Invoke member thread safe
body: <p>I have this service that, when request is received, runs a powershell command and returns result. Here is the invoker class code:</p>
  
  <pre><code>public class PowerShellScript {
  
      public PowerShellScript() {
      }
  
      public Object[] Invoke( String strScriptName, NameValueCollection nvcParams ) {
          Boolean bResult = true;
          int n = 0;
          Object[] objResult = null;
          PowerShell ps = PowerShell.Create();
          String strScript = strScriptName;
  
          for (n = 0; n &lt; nvcParams.Count; n++) {
              strScript += String.Format( " -{0} {1}", nvcParams.GetKey( n ), nvcParams[n] );
          }
  
          //ps.AddScript( @"E:\snapins\Init-profile.ps1" );
          ps.AddScript( strScript );
          Collection&lt;PSObject&gt; colpsOutput = ps.Invoke();
          if (colpsOutput.Count &gt; 0)
              objResult = new Object[colpsOutput.Count];
  
          n = 0;
          foreach (PSObject psOutput in colpsOutput) {
              if (psOutput != null) {
                  try {
                      objResult[n] = psOutput.BaseObject;
                  }
                  catch (Exception ex) { 
                      //exception should be handeled properly in powershell script
                  }
              }
              n++;
          }
          colpsOutput.Clear();
          ps.Dispose();
  
          return objResult;
      }
  }
  </code></pre>
  
  <p>Method Invoke returns all results returned by powershell script.</p>
  
  <p>All fine and well. As long as this runs in a single thread. As some powershell scripts we invoke can take up to an hour to complete and we don't want for service to do nothing in that time, we decided to go multi-threaded.
  Unfortunately Powershell class is not thread safe, resulting in sever memory leaks and cpu burn rate. However, if I use lock on Invoke method, this would mean that the entire idea why we went multithreaded will go down the drain.</p>
  
  <p>Any ideas how to solve this?</p>