C# 如何执行powershell调用两次?

C# 如何执行powershell调用两次?,c#,asp.net,powershell,C#,Asp.net,Powershell,我可以调用powershell结果一次,但当我添加第二个命令再次调用时,它会得到一个错误异常,我无法再次调用,因为它已在前面调用(pipeline.invoke();)。当我需要执行第一个命令以获得我需要使用的结果,然后再次执行时,如何使其工作?我已经尝试了pipeline.Dispose()和pipeline.Commands.Clear()它们不工作 这里有一个C代码 protectedvoid按钮请求\单击(对象发送者,事件参数e) { HiddenName.Visible=false;

我可以调用powershell结果一次,但当我添加第二个命令再次调用时,它会得到一个错误异常,我无法再次调用,因为它已在前面调用(
pipeline.invoke();
)。当我需要执行第一个命令以获得我需要使用的结果,然后再次执行时,如何使其工作?我已经尝试了
pipeline.Dispose()
pipeline.Commands.Clear()它们不工作

这里有一个C代码

protectedvoid按钮请求\单击(对象发送者,事件参数e)
{
HiddenName.Visible=false;
字符串str=“”;
字符串ipAddress=“”;
字符串名称=”;
变量tbids=(列表)会话[“tbids”];
//创建一个powershell
Runspace Runspace=RunspaceFactory.CreateRunspace();
Open();
RunspaceInvoke=new RunspaceInvoke();
Pipeline Pipeline=runSpace.CreatePipeline();
命令invokeScript=新命令(“调用命令”);
//将powershell脚本文件和参数添加到scriptblock中
ScriptBlock sb=invoke.invoke(@“{D:\Scripts\Get-FreeAddress.ps1'”+DropDownListContainer.SelectedValue+“'”+DropDownListIP.SelectedValue+“}”)[0]。BaseObject作为ScriptBlock;
//ScriptBlock sb=invoke.invoke(“{”+PowerShellCodeBox.Text+“}”)[0]。BaseObject作为ScriptBlock;
invokeScript.Parameters.Add(“scriptBlock”,sb);
invokeScript.Parameters.Add(“computername”,TextBoxServer.Text);
pipeline.Commands.Add(invokeScript);
集合输出=pipeline.Invoke();
foreach(输出中的PSObject PSObject)
{
ipAddress=ipAddress+psObject;
foreach(TBID中的变量id)
{
尝试
{
//str+=请求[id]+“\r\n”;
名称=请求[id];
命令invokeScript2=新命令(“调用命令”);
//将powershell脚本文件和参数添加到scriptblock中
ScriptBlock sb2=invoke.invoke(@“{D:\Scripts\Set-IPAddress.ps1'+IPAddress+“'+name+“}”)[0]。BaseObject作为ScriptBlock;
invokeScript2.Parameters.Add(“脚本块”,sb2);
invokeScript2.Parameters.Add(“computername”,TextBoxServer.Text);
pipeline.Commands.Add(invokeScript2);
pipeline.Invoke();
}
抓住
{
}
}
}

您调用的调用与我以前看到的不同。我认为您的调用可以简化

大概是这样的:

传入:

@"{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();
}
私有字符串RunPowerShellScript(字符串scriptText)
{
//创建Powershell运行空间
Runspace Runspace=RunspaceFactory.CreateRunspace();
//打开它
Open();
//创建一个管道并向其提供脚本文本
Pipeline Pipeline=runspace.CreatePipeline();
pipeline.Commands.AddScript(scriptText);
//添加一个额外的命令来转换脚本
//将对象输出为格式良好的字符串
//删除此行以获取实际对象
//脚本返回的值。例如,脚本
//“获取进程”返回一个集合
//系统。诊断。过程实例的。
pipeline.Commands.Add(“输出字符串”);
//执行脚本
收集结果=pipeline.Invoke();
//关闭运行空间
runspace.Close();
//将脚本结果转换为单个字符串
StringBuilder StringBuilder=新的StringBuilder();
foreach(结果中的PSObject对象)
{
stringBuilder.AppendLine(obj.ToString());
}
返回stringBuilder.ToString();
}

您能发布您收到的确切错误消息吗?它说捕获了InvalidPipelineStateException,
无法调用管道,因为它已在前面调用过
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();
}