locked
Maintaining Powershell runspace across multiple pages RRS feed

  • Question

  • User1901911419 posted

    Hello,

    I am tyring to keep a powershell runspace open for Exchange online across multiple pages.  I am new to C# so appologies if this is a simple question.  Here is the sceanrio. I have directing folks to a login page which contains the following method that keeps the session open and then redirects to another page.

     

    protected void setupConnection (object sender, EventArgs e)

            {       

         string userName = "O365UserName";           

         string password = "O365Password";

              System.Security.SecureString securePassword = new System.Security.SecureString();

              foreach (char c in password)

              {

                    securePassword.AppendChar(c);

              }

             PSCredential credential = new PSCredential(userName, securePassword);

             WSManConnectionInfo connectionInfo = new WSManConnectionInfo(new Ur ("https://ps.outlook.com/powershell"), "http://schemas.microsoft.com/powershell/Microsoft.Exchange", credential);

             connectionInfo.AuthenticationMechanism =AuthenticationMechanism.Basic;

             connectionInfo.MaximumConnectionRedirectionCount = 2;

             using (Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo))

             {

                runspace.Open();

          using (PowerShell powershell = PowerShell.Create())

                 {

                      powershell.Runspace = runspace;

                      Session["psSessionVar"] = powershell.Runspace;

                      Response.Redirect ("ExTrasport.aspx");

                    }

                }

     

     

    After the page is redirected page to ExTransport.aspx I want the post the results of the transport rules that are shown in Exchange Online.  Here is what I wrote. 

     

    protected void Page_Load(object sender, EventArgs e)

            {

                  powershell = Session ["psSessionVar"];

                  powershell.AddCommand("Get-TransportRule");

                  Collection<PSObject> results = powershell.Invoke();
        }

     

     

    By doing this I am getting the following error: "The Name powershell does not exist in the current context".  Adding all of this onto a simgle page works with no problem.  So the quesiton is what is the best way to me to open a runspace and maintain it across multiple pages.

    Thursday, May 16, 2013 12:18 PM

Answers

  • User-1818759697 posted

    Hi,

    First you probably need to change this <asp:linkbutton id="addRule" onClick="newRule"> to <asp:linkbutton id="addRule" onClick="addRule"> .

    Second, you could use session to share data between pages, and for more details you could refer Save Values in Session State  Read Values from Session State and Pass Values Between Pages.

    Besides, I'm not sure what's your purpose of this code:

    powershell = (PowerShell)Session["psRunspaceSession"];
    
    Session["psRunspaceSession"] = powershell;

    Regards
     

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 23, 2013 1:34 AM

All replies

  • User1901911419 posted

    I was finally able to get it to work.  Here is what I did. 

    In the initial page I took out the 'using' so I could maintian the session in memory.  On the second page I needed to add the following to my code

                        PowerShell powershell = PowerShell.Create();

                        powershell = (PowerShell)Session["psRunspaceSession"];

                        powershell.AddCommand("Get-TransportRule");

                        Collection<PSObject> results = powershell.Invoke();

           So the next question is how do I maintain this throughout mulitple pages. I tried to create the following link from the ExTransport.aspx page.

    <asp:linkbutton id="addRule" onClick="newRule">

     

    One C# side it looks like this.    

    protected void addRule(object sender, EventArgs e)

            {

               PowerShell powershell = PowerShell.Create();

                powershell = (PowerShell)Session["psRunspaceSession"];

                Session["psRunspaceSession"] = powershell;

                Response.Redirect("AddRule.aspx");

            }

    So far this does not work.  Any help would be really appreciated. 

     

     

     

    Thursday, May 16, 2013 10:33 PM
  • User-1818759697 posted

    Hi,

    First you probably need to change this <asp:linkbutton id="addRule" onClick="newRule"> to <asp:linkbutton id="addRule" onClick="addRule"> .

    Second, you could use session to share data between pages, and for more details you could refer Save Values in Session State  Read Values from Session State and Pass Values Between Pages.

    Besides, I'm not sure what's your purpose of this code:

    powershell = (PowerShell)Session["psRunspaceSession"];
    
    Session["psRunspaceSession"] = powershell;

    Regards
     

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 23, 2013 1:34 AM