2010-04-07 92 views
2

我想要的參數從C#web應用程序傳遞給PowerShell的,但要得到一個錯誤:參數傳遞到PowerShell與C#

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."}

我的PowerShell腳本如下:

Param($ds) 
write-host $ds 

我的C#是:

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<PSObject> 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; 
} 

我跟着一些其他的線程,但不能腳本來執行。如果我從PowerShell控制檯運行PowerShell腳本,則只需調用腳本和參數即可執行PowerShell腳本。如果我從PowerShell腳本中刪除Param($ds),它不會報錯。

任何人都可以幫忙嗎?

謝謝。

回答

3

只有scriptblocks,ps1/psm1文件和函數/過濾器可以有一個param塊。你應該AddScript被添加腳本應該是這樣的形式:

& { param($ds); $ds } 

&是呼叫運營商。在你的例子中,你正試圖執行param作爲命令。

更新

您必須傳遞參數給腳本塊,像這樣:

& {參數($ DS); $ ds} 42

這導致42被傳遞到腳本塊。使用AddScript腳本不會隱式創建ps1文件。這類似於你打字:

ps c:\> param($ds); $ds 

...直接在提示符下;這是沒有意義的。合理?

-Oisin

+0

感謝您的回覆,但似乎並沒有出現問題。如果我有一個名爲test.ps1具有以下 參數($味精)腳本 寫主機$味精 ,然後用 ./test.ps1你好 稱它爲我得到打招呼寫入安慰。我也嘗試使用$ args [0] int腳本獲得相同的結果。如果我把 &{param($ ds); $ ds | out-file c:\ scripts \ testparam.txt} 進入腳本我打電話我仍然得到相同的錯誤。 – 2010-04-08 07:20:59

+0

您必須像這樣將參數傳遞給scriptblock: &{param($ ds); $ ds} 42 這導致42被傳遞給腳本塊。使用AddScript腳本不會隱式創建ps1文件。這類似於你輸入: ps c:\> param($ ds); $ ds 直接在提示符處。這沒有意義。合理? – x0n 2010-04-08 17:10:38

+0

很酷,現在它確實。謝謝。 – 2010-04-09 08:31:51