gpt4 book ai didi

c# - 如何执行两次powershell invoke?

转载 作者:行者123 更新时间:2023-11-30 18:31:28 28 4
gpt4 key购买 nike

我能够调用 powershell 结果一次,但是当我添加第二个命令再次调用时,它得到一个错误异常,我无法再次调用,因为它已经调用了较早的(pipeline.Invoke();)。当我需要执行第一个以获得我需要使用的结果然后再次执行它时,我怎样才能让它工作?我已经尝试过 pipeline.Dispose();pipeline.Commands.Clear(); 它们不起作用。

这里是C#代码,

protected void ButtonRequest_Click(object sender, EventArgs e)
{
HiddenName.Visible = false;
string str = "";
string ipAddress = "";
string name = "";
var tbids = (List<string>)Session["tbids"];

//create a powershell
Runspace runSpace = RunspaceFactory.CreateRunspace();
runSpace.Open();

RunspaceInvoke invoke = new RunspaceInvoke();

Pipeline pipeline = runSpace.CreatePipeline();

Command invokeScript = new Command("Invoke-Command");

//Add powershell script file and arguments into scriptblock
ScriptBlock sb = invoke.Invoke(@"{D:\Scripts\Get-FreeAddress.ps1 '" + DropDownListContainer.SelectedValue + "' " + DropDownListIP.SelectedValue + "}")[0].BaseObject as ScriptBlock;
//ScriptBlock sb = invoke.Invoke("{" + PowerShellCodeBox.Text + "}")[0].BaseObject as ScriptBlock;

invokeScript.Parameters.Add("scriptBlock", sb);

invokeScript.Parameters.Add("computername", TextBoxServer.Text);

pipeline.Commands.Add(invokeScript);

Collection<PSObject> output = pipeline.Invoke();

foreach(PSObject psObject in output)
{
ipAddress = ipAddress + psObject;

foreach (var id in tbids)
{
try
{
//str += Request[id] + "\r\n";
name = Request[id];

Command invokeScript2 = new Command("Invoke-Command");
//Add powershell script file and arguments into scriptblock
ScriptBlock sb2 = invoke.Invoke(@"{D:\Scripts\Set-IPAddress.ps1 '" + ipAddress + "' " + name + "}")[0].BaseObject as ScriptBlock;

invokeScript2.Parameters.Add("scriptBlock", sb2);

invokeScript2.Parameters.Add("computername", TextBoxServer.Text);

pipeline.Commands.Add(invokeScript2);

pipeline.Invoke();
}
catch
{

}
}
}

最佳答案

你调用的是我以前见过的不同的方法。我认为您的调用可以简化。

像这样:

传入:

@"{D:\Scripts\Get-FreeAddress.ps1 '" + DropDownListContainer.SelectedValue + "' " + DropDownListIP.SelectedValue + "}"

进入这个:

private string RunPowerShellScript(string scriptText)
{
// create Powershell runspace
Runspace runspace = RunspaceFactory.CreateRunspace();
// open it
runspace.Open();
// create a pipeline and feed it the script text
Pipeline pipeline = runspace.CreatePipeline();
pipeline.Commands.AddScript(scriptText);
// add an extra command to transform the script
// output objects into nicely formatted strings
// remove this line to get the actual objects
// that the script returns. For example, the script
// "Get-Process" returns a collection
// of System.Diagnostics.Process instances.
pipeline.Commands.Add("Out-String");
// execute the script
Collection<PSObject> results = pipeline.Invoke();
// close the runspace
runspace.Close();
// convert the script result into a single string

StringBuilder stringBuilder = new StringBuilder();
foreach (PSObject obj in results)
{
stringBuilder.AppendLine(obj.ToString());
}
return stringBuilder.ToString();
}

关于c# - 如何执行两次powershell invoke?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20131551/

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