2012-06-06 41 views
0

我有代碼從包含很多powershell cmdlet的.ps1文件運行powershell cmdlet ... when我執行它,我得到這個例外代碼來運行cmps的.ps1文件中的powershell cmdlet(函數?)

「新BuildCVFromSql」不被識別爲一個 cmdlet,函數,腳本文件或可操作的程序的名稱術語。檢查名稱的拼寫 ,或者如果包含路徑,請確認路徑爲 正確,然後重試。

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 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<PSObject> output = pipeline.Invoke(); 
    //foreach (PSObject psObject in output) 
    //{ 

    //System.Diagnostics.Debug.WriteLine ("Object name: " + psObject.); 
    //} 
} 

所以顯然我沒有正確加載腳本文件?在powershell ISE/IDE中,我必須在執行任何命令之前運行腳本,是我做錯了什麼?

我只是想把接口放在一堆由somone寫的powershell腳本上,所以我不能真的改變他的腳本文件,因爲他們在其他人的別處使用...

在一側的.ps1文件我試圖打電話給小命令正是如此定義:

# 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) 

...    

附錄:

Problem with calling a powershell function from c#

:使用PowerShell的而不是每個此帖一管道也試過

這樣,但我得到了同樣的異常:

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<string, string>() 
     { 
      { "SqlOrExcelFile", @"C:\tempeval.sql" }, 
      { "Js", "JCDC" }, 
      { "FileName", @"Evaluation\EvaluationFormPartialListCV" } 
     }); 

     foreach (PSObject result in ps.Invoke()) 
     { 
      Debug.WriteLine ("Object : " + result); 
     } 
    } 

附錄2:

刪除所有命令的東西(因爲他們只爲內置的cmdlet,而不是用戶自定義函數的工作),並這樣做反而似乎是在調用代碼,儘管當它運行時沒有任何依賴關係被覆蓋。仍然在研究這些。

ps.AddScript(@"New-BuildCVFromSql 'C:\Documents and Settings\Brown.Ericw\My Documents\tempeval.sql' JCDC 'Evaluation\EvaluationFormPartialListCV'"); 

似乎在調用代碼,儘管在運行時不會加載任何依賴關係..仍在研究這個問題。但是,如果它是從它的工作ISE ....運行

+0

任何具有完整源代碼的最終解決方案? – Kiquenet

+0

Down投票贊成不發佈剪切和粘貼答案?哇,這是苛刻的......它只有1行不同,並在評論中。 –

回答

2

在你的RunPowerShellCommandToBuildCV()第二個版本是缺少ps.invoke()添加腳本後:

//load as file - EWB 
    ps.AddScript(psScriptPath); 
    ps.Invoke(); //<--------- !!!! 
    ps.AddCommand("New-BuildCVFromSql").AddParameters(new Dictionary<string, string>() 
    { 
     { "SqlOrExcelFile", @"C:\tempeval.sql" }, 
     { "Js", "JCDC" }, 
     { "FileName", @"Evaluation\EvaluationFormPartialListCV" } 
    }); 

Invoke()runspace知道有一個叫功能New-BuildCVFromSql

+0

嗯我試過了,它得到和以前一樣的異常... –

+0

我想出了ps.AddScript(psScriptPath);需要是ps.AddScript(psScript); (通過文件名加載文件不加載的文本),它正在執行,雖然它沒有看到我的參數...看着 –

+0

@ EricBrown-Cal爲什麼你傳遞一個'Dictionary '。看到你的函數參數聲明,我似乎有所有的字符串。 –

0

這就是我這樣做,工作一種享受。

private static void Test() 
{ 
    dynamic parameters = new ExpandoObject(); 
    parameters.test= "myImage"; 
    parameters.arg2= 2; 

    var results = RunPowerShellFunction("My-Function", parameters); 
    var obj = results[0]; 
    var str = results[1]; 
} 

private static dynamic RunPowerShellFunction(string functionName, dynamic parameters) 
{ 
    dynamic rv = null; 

    try 
    { 
     InitialSessionState iss = InitialSessionState.CreateDefault(); 

     using (Runspace runspace = RunspaceFactory.CreateRunspace(iss)) 
     { 
      runspace.Name = typeof(DockerManager).Name; 
      runspace.Open(); 

      RunspaceInvoke runSpaceInvoker = new RunspaceInvoke(runspace); 
      runSpaceInvoker.Invoke("Set-ExecutionPolicy Unrestricted"); 

      using (var mainPowerShell = System.Management.Automation.PowerShell.Create()) 
      { 
       mainPowerShell.Runspace = runspace; 

       mainPowerShell.AddScript(LoadedScriptText, false); 
       mainPowerShell.Invoke(); 

       var cmd = mainPowerShell.AddCommand(functionName); 

       if (parameters != null) 
       { 
        foreach (var parameter in parameters) 
        { 
         cmd.AddParameter(parameter.Key, parameter.Value); 
        } 
       } 

       rv = cmd.Invoke(); 
      } 
     } 
    } 
    catch (Exception ex) 
    { 
     Debug.WriteLine(ex.Message); 
    } 

    return rv; 
} 
相關問題