2

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();

    }
}
1
  • What does your current code look like? Commented Feb 15, 2015 at 16:43

1 Answer 1

1

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'")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.