如何执行powershell两次调用?

时间:2013-11-21 20:48:13

标签: c# asp.net powershell

我能够一次调用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
                {

                }
            }
        }

1 个答案:

答案 0 :(得分:0)

你正在调用我以前见过的不同。我认为你的调用可以简化。

这样的事情:

传递:

@"{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();
}