传递参数从C#的PowerShell [英] passing parameters to powershell from c#
问题描述
我想传递一个参数从C#web应用程序的PowerShell,但不断收到错误
I'm trying to pass a parameter to powershell from c# web app, but keep getting an error
原因= {术语参数($ DS) \r\\\
\r\\\
$ ds\r\\\
\r\\\
\r\\\
未被识别为cmdlet,函数的名称,脚本文件或可操作的。程序检查名称的拼写,或者是否包含路径,验证路径是否正确,然后再试一次}
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脚本如下:
my powershell script is as follows:
参数($ DS)
写主机$ 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脚本它没有错误删除参数($ DS)。
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 Param($ds) from the powershell script it doesn't error.
谁能帮助?
感谢。
推荐答案
只有scriptblocks,PS1 / PSM1文件和函数/过滤器可以有一个参数块。你应该AddScript被添加脚本应该是下面的形式:
Only scriptblocks, ps1/psm1 files and functions/filters can have a param block. The script you should be adding with AddScript should be of the form:
& { param($ds); $ds }
&安培;是呼叫接线员。在你的榜样,您要执行参数为命令。
& is the call operator. In your example, you are trying to execute param as a command.
更新
您必须传递参数给脚本块,像这样:
You must pass the arguments to the scriptblock like so:
&安培; {参数($ DS); $ DS} 42
& { param($ds); $ds } 42
这将导致42是passesd的脚本块。在坚持脚本AddScript不会隐式创建一个PS1文件。这类似于你键入:
This results in 42 being passesd to the scriptblock. Sticking script in with AddScript doesn't implicitly create a ps1 file. It's akin to you typing:
ps c:\> param($ds); $ds
...直接在提示;这是没有意义的。有意义吗?
...directly at the prompt; this is meaningless. Make sense?
-Oisin
这篇关于传递参数从C#的PowerShell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!