title: C# Runspace Powershell (Interactive)
body: <p>I am trying with C# to execute a Powershell file with paramters in a runspace. Unfortunately i get the following output:</p>
  
  <blockquote>
    <p>A command that prompts the user failed because the host program or the
    command type does not support user interaction. Try a host program
    that supports user interaction, such as the Windows PowerShell Console
    or Windows PowerShell ISE, and remove prompt-related commands from
    command types that do not support user interaction, such as Windows
    PowerShell workflows.</p>
  </blockquote>
  
  <p>What could i do? </p>
  
  <p>Current c# code. this needs to execute commands that will be in a PS file and needs to return a json string. </p>
  
  <pre><code>public string ExecuteCommandDirect(int psId, string psMaster, string psFile)
  {
      String FullPsFilePath = @"C:\CloudPS\" + psFile + ".ps1";
  
      String PsParameters = FullPsFilePath + " -psId " + psId + " -psMaster " + psMaster + " -NonInteractive";
  
      // Create Powershell Runspace
      Runspace runspace = RunspaceFactory.CreateRunspace();
  
      runspace.Open();
  
      // Create pipeline and add commands
      Pipeline pipeline = runspace.CreatePipeline();
      pipeline.Commands.AddScript(PsParameters);
  
      // Execute Script
      Collection&lt;PSObject&gt; results = new Collection&lt;PSObject&gt;();
      try
      {
          results = pipeline.Invoke();
      }
      catch (Exception ex)
      {
          results.Add(new PSObject((object)ex.Message));
      }
  
      // Close runspace
      runspace.Close();
  
      //Script results to string
      StringBuilder stringBuilder = new StringBuilder();
      foreach (PSObject obj in results)
      {
          Debug.WriteLine(obj);
          stringBuilder.AppendLine(obj.ToString());
      }
  
      return stringBuilder.ToString();
  
  }
  </code></pre>
  
  <p>PS Code:</p>
  
  <pre><code>param([int]$psId = 0, [string]$psMaster = 'localhost');
  
  $date = Get-Date -Format 'h:m:s' | ConvertTo-Json;
  
  Write-Host $date;
  
  exit;
  </code></pre>