简体   繁体   中英

Keeping a powershell runspace alive in C# WPF?

I'm planning on building an Active Directory/Exchange admin console using C# talking powershell to DC and Exchange servers.

I want to on application launch establish powershell connections to these servers and then keep them alive so I can keep running queries or scripts or whatever because it takes a couple of seconds to establish the remote connection and it just won't work to have that kind of delay on everything you do.

I'm currently just testing a local powershell runspace but every time I send a command to it it closes and I can't reuse it after the initial command.

How can I prevent the runspace from closing so I can use it over and over again?

edit: code Very basic, just creating a runspace, planning on being able to include modules later on when I've got the basic functionality down. The idea was to create a runspace and when calling the function that executes powershell code assign that runspace to another variable so I could reuse it but I'm probably stupid. Currently I just have a dummy "Get-Process" that's sent when clicking a button and a textbox that displays the output.

public partial class MainWindow : Window
{
    Runspace powerShellRunspace = RunspaceFactory.CreateRunspace();
    public MainWindow()
    {

        InitializeComponent();
        powerShellRunspace.Open();
        string[] modules;
        scriptOutput.Text = "test";
        modules = new string[5];
        modules[0] = "john";



        //string result = powerShellRun("Get-Process");
        //powerShellInitialize(modules);

    }

    public static void powerShellInitialize(string[] modules)
    {
        Runspace powerShellRunspace = RunspaceFactory.CreateRunspace();
        powerShellRunspace.Open();

    }

    public string powerShellRun(string commands, Runspace powerShellRunspace)
    {

        Runspace powerShellRunspace2 = powerShellRunspace;
        Pipeline powerShellPipeline = powerShellRunspace2.CreatePipeline();
        powerShellPipeline.Commands.Add(commands);
        Collection<PSObject> powerShellResult = powerShellPipeline.Invoke();
        //string result="temp";
        //return result;
        StringBuilder stringBuilder = new StringBuilder();
        foreach (PSObject obj in powerShellResult)
        {
            stringBuilder.AppendLine(obj.ToString());
        }

        return stringBuilder.ToString();

    }
}

This question was already answered on Keeping Powershell runspace open in .Net

In summary, you can keep your Runspace Open, but for each independent query, you need to create a new Powershell instance.

Example:

Runspace runspace = RunspaceFactory.CreateRunspace(initial);
runspace.Open();

//First Query
var firstQuery = PowerShell.Create();
firstQuery.Runspace = runspace;
firstQuery.AddScript("Write-Host 'hello'")

//Second Query
var secondQuery = PowerShell.Create();
secondQuery.Runspace = runspace;
secondQuery.AddScript("Write-Host 'world'")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM