title: passing parameters to powershell from c#
body: <p>I'm trying to pass a parameter to PowerShell from C# web app, but keep getting an error:</p>
  
  <blockquote>
    <p>Reason = {"The term 'Param($ds)\r\n\r\n$ds\r\n\r\n\r\n' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again."}</p>
  </blockquote>
  
  <p>My Powershell script is as follows:</p>
  
  <pre><code>Param($ds)
  write-host $ds
  </code></pre>
  
  <p>My C# is:</p>
  
  <pre><code>protected void drpCluster_SelectedIndexChanged(object sender, EventArgs e)
  {
      // Looping through all the rows in the GridView
      foreach (GridViewRow row in GridVMApprove.Rows)
      {
          if (row.RowState == DataControlRowState.Edit)
          {
              // create dynamic dropDowns for datastores
              DropDownList drpDatastore = (DropDownList)row.FindControl("drpDatastore");
              DropDownList drpCluster = (DropDownList)row.FindControl("drpCluster");
              var Datacenter = "'" + drpCluster.SelectedValue + "'";
              strContent = this.ReadPowerShellScript("~/scripts/Get-DatastoresOnChange.ps1");
              this.executePowershellCommand(strContent, Datacenter);
              populateDropDownList(drpDatastore);
          }
      }
  }
  public string ReadPowerShellScript(string Script)
  {
      // Read script
      StreamReader objReader = new StreamReader(Server.MapPath(Script));
      string strContent = objReader.ReadToEnd();
      objReader.Close();
      return strContent;
  }
  private string executePowershellCommand(string scriptText, string scriptParameters)
  {
      RunspaceConfiguration rsConfig = RunspaceConfiguration.Create();
      PSSnapInException snapInException = null;
      PSSnapInInfo info = rsConfig.AddPSSnapIn("vmware.vimautomation.core", out snapInException);
      Runspace RunSpace = RunspaceFactory.CreateRunspace(rsConfig);
      RunSpace.Open();
      Pipeline pipeLine = RunSpace.CreatePipeline();
      Command scriptCommand = new Command(scriptText); 
      pipeLine.Commands.AddScript(scriptText);
      if (!(scriptParameters == null))
      {
          CommandParameter Param = new CommandParameter(scriptParameters);
          scriptCommand.Parameters.Add(Param);
          pipeLine.Commands.Add(scriptCommand);              
      }
      // Execute the script
      Collection&lt;PSObject&gt; commandResults = pipeLine.Invoke();
      // Close the runspace
      RunSpace.Close();
      // Convert the script result into a single string
      System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
      foreach (PSObject obj in commandResults)
      {
          stringBuilder.AppendLine(obj.ToString());
      }
      OutPut = stringBuilder.ToString();
      return OutPut;
  }
  </code></pre>
  
  <p>I've followed some other threads but can't script to execute. My PowerShell scripts executes if I run it from PowerShell console just calling the script and a parameter. If I remove <code>Param($ds)</code> from the PowerShell script it doesn't error.</p>
  
  <p>Can anyone help?</p>
  
  <p>Thanks.</p>