title: Parameter not being passed from code
body: <p>I am working on a simple SharePoint page that gets data from a dropdown and passes them to a PowerShell script as a parameter. But I get an error when debugging, and I found out that there are no parameters being passed to the script.</p>
  
  <p>The script file is placed inside my layouts folder, with a structure like this:</p>
  
  <pre class="lang-psh prettyprint-override"><code>Param($param1, $param2, $param3)
  </code></pre>
  
  <p>What I am doing from my C# code is I read the file using a <code>StreamReader</code> and then run the script using this code:</p>
  
  
  
  <pre class="lang-cs prettyprint-override"><code>using (PowerShell powerShell = PowerShell.Create()) {
      powerShell.Runspace.RunspaceConfiguration.AddPSSnapIn(_powershell,out snapInException);           
      powerShell.Commands.AddScript(the read script form streamreader); 
      powerShell.AddParameter("param1", SPContext.Current.Site.Url + "/");
      powerShell.AddParameter("param2", selectedText1);
      powerShell.AddParameter("param3", selectedText2);
  
      try {
          var results = powerShell.Invoke();
      } catch (Exception exp) { }                     
  }
  </code></pre>
  
  <p>I am getting an error that my parameters are null and I don't know why my parameters are not passing. Am I doing something wrong? </p>
  
  <p>Also, I tried running the ps1 script using this one:</p>
  
  <pre class="lang-cs prettyprint-override"><code>RunspaceConfiguration runspaceCOnfig = RunspaceConfiguration.Create();
  Runspace runspace = RunspaceFactory.CreateRunspace(runspaceCOnfig);
  runspace.Open();
  
  RunspaceInvoke scriptInvoker = new RunspaceInvoke(runspace);
  Pipeline pipeline = runspace.CreatePipeline();
  Command myCommand = new Command(script path);
  
  CommandParameter param1 = new CommandParameter("param1", SPContext.Current.Site.Url + "/");
  CommandParameter param2 = new CommandParameter("param2", selectedText1);
  CommandParameter param3 = new CommandParameter("param3", selectedText2);
  myCommand.Parameters.Add(param1);
  myCommand.Parameters.Add(param2);
  myCommand.Parameters.Add(param3);
  
  pipeline.Commands.Add(myCommand);
  </code></pre>
  
  <p>and I get this error:</p>
  
  <blockquote>
    <p>'path_to_script.ps1' 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>