title: Code to run a PowerShell cmdlet (function?) that lives in a .ps1 file of cmdlets (functions?)
body: <p>I've got code to run a PowerShell cmdlet from a .ps1 file containing a lot of PowerShell cmdlets... when I execute it I get this exception:</p>
  
  <blockquote>
    <p>The term 'New-BuildCVFromSql' 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>
  
  <pre><code>private void RunPowerShellCommandToBuildCV()
  {
      string BigWindow = "";
      string FileNamePopup = "";
  
      TestPopup(ref BigWindow, ref FileNamePopup);
  
      string psScriptPath = @"D:\Project Files\CIS3G\Webapp\_Powershell\JcdcCv.psm1";
      string psScript = string.Empty;
  
      if(File.Exists(psScriptPath))
          psScript = File.ReadAllText(psScriptPath);
      else
          throw new FileNotFoundException("Wrong path for the script file");
  
      // Init the PowerShell runspace and pipeline - EWB
      Runspace runSpace = RunspaceFactory.CreateRunspace();
      runSpace.Open();
  
      // Set execution policy - EWB
      RunspaceInvoke runSpaceInvoker = new RunspaceInvoke(runSpace);
      runSpaceInvoker.Invoke("Set-ExecutionPolicy Unrestricted");
  
      Pipeline pipeline = runSpace.CreatePipeline();
  
      // I tried loading it both of the ways below and had no joy load the script from the string - EWB
      //pipeline.Commands.AddScript(psScript);
  
      // Load as file - EWB
      pipeline.Commands.AddScript(psScriptPath, false);
  
      // Add the outstring command to the pipeline.
      pipeline.Commands.Add("Out-String");
  
      Command myCommand = new System.Management.Automation.Runspaces.Command("New-BuildCVFromSql");
  
      CommandParameter SqlOrExcelFile                          = new CommandParameter("SqlOrExcelFile", @"C:\Documents and Settings\Brown.Ericw\My Documents\tempeval.sql");
      CommandParameter Js                                      = new CommandParameter("Js", "JCDC");
      CommandParameter FileName                                = new CommandParameter("FileName", @"Evaluation\EvaluationFormPartialListVM");
  
      myCommand.Parameters.Add(SqlOrExcelFile);
      myCommand.Parameters.Add(Js);
      myCommand.Parameters.Add(FileName);
  
      pipeline.Commands.Add(myCommand);
  
      Collection&lt;PSObject&gt; output = pipeline.Invoke();
      //foreach (PSObject psObject in output)
      //{
  
      //System.Diagnostics.Debug.WriteLine ("Object name: " + psObject.);
      //}
  }
  </code></pre>
  
  <p>So apparently I'm not correctly loading this file of scripts. In PowerShell ISE/IDE I have to run the script before I can execute any of the commands. Is that what I'm doing wrong?</p>
  
  <p>I'm just trying to put an interface on top of a bunch of PowerShell scripts written by someone else, so I can't really change his/her script files, as they are being used elsewhere by other folks...</p>
  
  <p>Inside the .ps1 file the cmdlet I'm trying to call is defined thusly:</p>
  
  <pre><code># Create CV Method #
  
  function New-BuildCVFromSql {
    param([Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true)]$SqlFile,
          [Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true)]$js,
          [Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true)]$FileName)
  
   ...
  </code></pre>
  
  <p>Addendum: I also tried using a PowerShell instead of a pipeline per this post:</p>
  
  <p><em><a href="https://stackoverflow.com/questions/7294765/problem-with-calling-a-powershell-function-from-c-sharp">Problem with calling a powershell function from c#</a></em></p>
  
  <p>Like this, but I got the same exception:</p>
  
  <pre><code>private void RunPowerShellCommandToBuildCV()
  {
      string BigWindow = "";
      string FileNamePopup = "";
  
      TestPopup(ref BigWindow, ref FileNamePopup);
  
      string psScriptPath = @"D:\Project Files\CIS3G\Webapp\_Powershell\JcdcCv.psm1";
      string psScript = string.Empty;
  
      if (File.Exists(psScriptPath))
          psScript = File.ReadAllText(psScriptPath);
      else
          throw new FileNotFoundException("Wrong path for the script file");
  
      // Init the PowerShell runspace and pipeline - EWB
      using (Runspace runSpace = RunspaceFactory.CreateRunspace())
      {
          runSpace.Open();
  
          // Set execution policy - EWB
          RunspaceInvoke runSpaceInvoker = new RunspaceInvoke(runSpace);
          runSpaceInvoker.Invoke("Set-ExecutionPolicy Unrestricted");
  
          //Pipeline pipeline = runSpace.CreatePipeline();
  
          PowerShell ps = PowerShell.Create();
          ps.Runspace = runSpace;
  
          // Load as file - EWB
          ps.AddScript(psScriptPath);
  
          ps.AddCommand("New-BuildCVFromSql").AddParameters(new Dictionary&lt;string, string&gt;()
           {
               {"SqlOrExcelFile", @"C:\tempeval.sql"},
               {"Js", "JCDC"},
               {"FileName", @"Evaluation\EvaluationFormPartialListCV"}
           });
  
          foreach (PSObject result in ps.Invoke())
          {
              Debug.WriteLine ("Object : " + result);
          }
      }
  </code></pre>
  
  <p>Addendum 2:</p>
  
  <p>Removing all the command stuff (because they only work for built in CmdLets, not user defined functions) and doing this instead seems to be calling the code though none of the dependencies are loaded when it's running... I am still looking into that.</p>
  
  <pre><code>ps.AddScript( @"New-BuildCVFromSql 'C:\Documents and Settings\Brown.Ericw\My Documents\tempeval.sql' JCDC 'Evaluation\EvaluationFormPartialListCV'" );
  </code></pre>
  
  <p>It seems to be calling the code though none of the dependencies are loaded when it's running... I am still looking into that. But if it's run from the ISE it works...</p>