0

I want to create a runspace with in some webpage code that a power shell session will run in. I want this run space to be created when the user opens the page up, and remain open until it is closed. So for example the code below is for one button click. but each time I run a power shell command i need to run the

"$S = new-pssesession -configurationname micros......"

This takes several seconds and is just setting up the session, Where can I create the runspace and powershell object so that I can call it from any where in the code with out having to recreate it each time I want to use it. In a console app I would pout in in the "main" class, but it seems web pages/applications have a different structure

protected void Button1_Click(object sender, EventArgs e)
    {

        var runspace = RunspaceFactory.CreateRunspace();
        runspace.Open();

        var powershell = PowerShell.Create();
        //{
        powershell.Runspace = runspace;


        powershell.AddScript("Set-ExecutionPolicy Unrestricted ; $s=new-pssession -configurationname microsoft.exchange -connectionuri http://server.com/powershell -authentication kerberos ; import-pssession $s");
        powershell.Invoke();
        powershell.AddScript("get-dynamicdistributiongroup");
        Collection<PSObject> resultsL = powershell.Invoke();


        // close the runspace 
        runspace.Close();

        Input.Items.Clear();
        foreach (var dlist in resultsL)
        { 
            Input.Items.Add(dlist.ToString());
        }

        Input.DataBind();

MY PROPOSED SOLUTION

So I thought I would try to make up a object that was static and I could make calls to. I thought that by creating a static calls that the first time I called it would run the "static powers()" construct, but any further calls would only call the section of code in the method. However not sure i have it quite right yet.

public static class powers
    {

        static public Runspace runspace = RunspaceFactory.CreateRunspace();
        static public PowerShell powershell = PowerShell.Create();

        static powers()
        {

            runspace.Open();
            powershell.Runspace = runspace;
            powershell.AddScript("Set-ExecutionPolicy Unrestricted ; $s=new-pssession -configurationname microsoft.exchange -connectionuri http://exchangeserver.com/powershell -authentication kerberos ; import-pssession $s");
            var resultL = powershell.Invoke();

        }

        static public Collection<PSObject> glist()
        {


            powershell.AddScript("get-dynamicdistributiongroup");
            Collection<PSObject> resultL = powershell.Invoke();
            return resultL;

        }

        static public Collection<PSObject> gmembers(string listn)
        {

            string GetMembers = "$FTE = Get-DynamicDistributionGroup '" + listn  + "'";
            powershell.AddScript(GetMembers);
            powershell.Invoke();
            powershell.AddScript("Get-Recipient -RecipientPreviewFilter $FTE.RecipientFilter");
            Collection<PSObject> resultM = powershell.Invoke();
            return resultM;

        }
    }
DevilWAH
  • 2,553
  • 13
  • 41
  • 57

1 Answers1

1

I've built several other applications where I've stored things in the session and used properties to access them. Something similar to this may work for you.

public static Runspace runspace
{
  get
  {
    //if not exist, create, open and store
    if (Session["runspace"] == null){
        Runspace rs = RunspaceFactory.CreateRunspace();
        rs.Open();
        Session["runspace"] = rs;
    }

    //return from session
    return (Runspace)Session["runspace"];
  }
}

Then you can simply access it as a static property in your event handler.

StephenP
  • 3,895
  • 18
  • 18