gpt4 book ai didi

c# - 从 C# 向 powershell 传递参数

转载 作者:行者123 更新时间:2023-11-30 22:46:34 25 4
gpt4 key购买 nike

我正在尝试从 C# 网络应用程序向 PowerShell 传递参数,但不断收到错误消息:

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 脚本中删除 Param($ds),它不会出错。

有人能帮忙吗?

谢谢。

最佳答案

只有脚本 block 、ps1/psm1 文件和函数/过滤器可以有参数 block 。您应该使用 AddScript 添加的脚本应该是以下形式:

& { param($ds); $ds }

& 是调用运算符(operator)。在您的示例中,您尝试将 param 作为命令执行。

更新

您必须像这样将参数传递给脚本 block :

& { 参数($ds); $ds } 42

这导致 42 被传递到脚本 block 。使用 AddScript 粘贴脚本不会隐式创建 ps1 文件。这类似于您键入:

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

...直接在提示符下;这是没有意义的。有道理吗?

-奥伊辛

关于c# - 从 C# 向 powershell 传递参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2594587/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com