title: Execute Powershell Script from C# with users input command line
body: <p>I created a website that does remotely execute powershell to specific server. I would like to know how it work to pass 2 user selected values to commandline from dropdownlist on asp.net website along with powershell script file?
  I already have working code with powershell script file but now adding 2 arguments in script file.The powershell arguments is,</p>
  
  <pre><code>    [string]$ContainerIn=$args[0]
  [int]$ips2get=$args[1]
  </code></pre>
  
  <p>Here a C# working codes,</p>
  
  <pre><code>//These 3 input varaibles will pass to powershell script to get specific results
          string env = "";
          string container = "";
          string numIPs = "";
  
          //assign dropdown selected value to variable to pass to script
          container = DropDownListContainer.SelectedValue;
          numIPs = DropDownListIP.SelectedValue;
  
          if (container == "H02" || container == "H07" || container == "H08")
          {
              env = "Prod";
          }
          else
          {
              env = "NonProd";
          }
  
          // Create a Powershell
          Runspace runSpace = RunspaceFactory.CreateRunspace();
          runSpace.Open();
          Pipeline pipeline = runSpace.CreatePipeline();
  
          Command invokeScript = new Command("Invoke-Command");
  
  
          RunspaceInvoke invoke = new RunspaceInvoke();
  
          //Add powershell command/script functions into scriptblock
          //Somewhere on this codes that it need to add command line to go with Get-FreeAddress.ps1 file script
  
          ScriptBlock sb = invoke.Invoke(@"{D:\Scripts\Get-FreeAddress.ps1}")[0].BaseObject as ScriptBlock;
          //ScriptBlock sb = invoke.Invoke("{" + PowerShellCodeBox.Text + "}")[0].BaseObject as ScriptBlock;
  
          invokeScript.Parameters.Add("scriptBlock", sb);
          invokeScript.Parameters.Add("computername", TextBoxServer.Text);
  
          pipeline.Commands.Add(invokeScript);
  
          Collection&lt;PSObject&gt; output = pipeline.Invoke();
  
          //splitting results in new lines
          foreach (PSObject psObject in output)
          {
  
              str = str + psObject + "\r\n";
              //str = psObject + "\r\n";
              //str += "\n" + psObject;
              //str = str + Environment.NewLine + psObject;
  
          }
  
          if (str == "")
          {
              str = "Error";
  
              ResultBox.ForeColor = System.Drawing.ColorTranslator.FromHtml("#FF0000");
          }
  
          //print out powershell output result
          ResultBox.Text = str;
  
      }
  </code></pre>