header
header Register : : Login header
header
divider
menuleft
menuright
submenu
left

[August 25th, 2008] Check the home page regarding PowerShell related news from a brand new sponsor: Idera

Subject: Powershell via ASPX - Pipeline problem
Prev Next
You are not authorized to post a reply.

Author Messages
KarlMitschkeUser is Offline
Power User
Power User
Posts:144

11/28/2007 10:15 AM  

I am creating a runspace and sending powershell commands from an aspx web form, and all works well unless some bozo double clicks the submit button, in which case I get an error that the pipeline is already executing. (OK, it was me)

I use the following from http://www.leastprivilege.com/HostingPowerShellInASPNET.aspx

protected Collection runposh(string strCommand)
    {
        Runspace rs = GetRunspace();
        Pipeline cmd = rs.CreatePipeline(strCommand);
        Collection results = cmd.Invoke();
        return (results);
    }
protected Runspace GetRunspace()
    {
        if (Cache["rs"] == null)
        {
             Runspace rs = RunspaceFactory.CreateRunspace();
             rs.Open();
             Cache["rs"] = rs;
        }
    return (Runspace)Cache["rs"]
    }

So, I thought I'd check the pipeline using:
protected Collection runposh(string strCommand)
    {
        Runspace rs = GetRunspace();
        try
        {

            if (currentPipeline == null || currentPipeline.PipelineStateInfo.State != PipelineState.Running)
            {
                currentPipeline = rs.CreatePipeline(strCommand);
                Collection results = currentPipeline.Invoke();
                return (results);
            }
            else
            {
                return (null);
            }
        }
        finally
        {
        }
    }
Which works pretty good unless the bozo (still me) rapidly clicks the submit button over and over like a rabid squirrel on energy drinks. - Then it brings up a blank page, and you have to refresh, and pass the data again, which works.

I'd not worry about this, but some of these people that will be using the program make me look calm, so I am sure they will break it too ;)

marco.shawUser is Offline
Co-Community Director
Power User
Power User
Posts:151

11/29/2007 10:55 AM  
How about trying this:
http://aspzone.com/blogs/john/articles/207.aspx

Marco

*Microsoft MVP - Windows PowerShell: http://www.microsoft.com/mvp
*PowerGadgets MVP: http://www.powergadgets.com/mvp
*Blog: http://marcoshaw.blogspot.com
KarlMitschkeUser is Offline
Power User
Power User
Posts:144

11/30/2007 9:56 AM  
Posted By KarlMitschke on 11/28/2007 10:15 AM

I am creating a runspace and sending powershell commands from an aspx web form, and all works well unless some bozo double clicks the submit button, in which case I get an error that the pipeline is already executing. (OK, it was me)

I use the following from http://www.leastprivilege.com/HostingPowerShellInASPNET.aspx

protected Collection runposh(string strCommand)
    {
        Runspace rs = GetRunspace();
        Pipeline cmd = rs.CreatePipeline(strCommand);
        Collection results = cmd.Invoke();
        return (results);
    }
protected Runspace GetRunspace()
    {
        if (Cache["rs"] == null)
        {
             Runspace rs = RunspaceFactory.CreateRunspace();
             rs.Open();
             Cache["rs"] = rs;
        }
    return (Runspace)Cache["rs"]
    }

So, I thought I'd check the pipeline using:
protected Collection runposh(string strCommand)
    {
        Runspace rs = GetRunspace();
        try
        {

            if (currentPipeline == null || currentPipeline.PipelineStateInfo.State != PipelineState.Running)
            {
                currentPipeline = rs.CreatePipeline(strCommand);
                Collection results = currentPipeline.Invoke();
                return (results);
            }
            else
            {
                return (null);
            }
        }
        finally
        {
        }
    }
Which works pretty good unless the bozo (still me) rapidly clicks the submit button over and over like a rabid squirrel on energy drinks. - Then it brings up a blank page, and you have to refresh, and pass the data again, which works.

I'd not worry about this, but some of these people that will be using the program make me look calm, so I am sure they will break it too ;)


I thoiught the aboce code didn't look right ;) That's what I use on the third page of this app. My main page, where I was having th problem attempted to cache the pipeline, thus:

protected Collection runposh(string strCommand)
{
 Runspace rs = GetRunspace();
        Pipeline currentPipeline = GetPipeline(rs, strCommand);
        if (currentPipeline.PipelineStateInfo.State == PipelineState.NotStarted)
         {
                 Collection results = currentPipeline.Invoke();
                 currentPipeline.Dispose();
                 Cache.Remove("currentPipe");
                 return (results);
             }
        else
         {
             return null;
         }

protected Runspace GetRunspace()
{
 if (Cache["rs"] == null)
         {
             Runspace rs = RunspaceFactory.CreateRunspace();
             rs.Open();
             Cache["rs"] = rs;
         }
         return (Runspace)Cache["rs"]
}
protected Pipeline GetPipeline(Runspace rs, string strCommand)
{
        if (Cache["currentPipe"] == null)
         {
             Pipeline currentPipeline = rs.CreatePipeline(strCommand);
             Cache["currentPipe"] = currentPipeline;
         }
        return (Pipeline)Cache["currentPipe"]
}

I honestly cannot remember if this one page works, so I will be testing that.
KarlMitschkeUser is Offline
Power User
Power User
Posts:144

11/30/2007 10:02 AM  
Posted By marco.shaw on 11/29/2007 10:55 AM
How about trying this:
http://aspzone.com/blogs/john/articles/207.aspx

Thanks, Marco!

I was searching for "BOZO USER TIRCK" ASP, and should have been searching "Stupid user trick" ASP ;)

No, I would have never found that page - thanks!

Initially, I thought it wouldn't work, as I create buttins programatically, so I cannot do anything tpo them in page_load, but this works at runtime

Button btnCreate = new Button();

btnCreate.Attributes.Add(
"onclick", "this.value='Please wait...';this.disabled= true;" + this.GetPostBackEventReference(btnCreate));

btnCreate.Text = "Create Mailbox";

frmStateMailboxConfirm.Controls.Add(btnCreate);

KarlMitschkeUser is Offline
Power User
Power User
Posts:144

12/03/2007 12:05 PM  
For anyone following this, it turns out that I am using an obsolete:

btnCreate.Attributes.Add("onclick", "this.value='Please wait...';this.disabled= true;" + this.GetPostBackEventReference(btnCreate));

Brings up a warning: 'System.Web.UI.Page.GetPostBackEventReference(System.Web.UI.Control)' is obsolete: 'The recommended alternative is ClientScript.GetPostBackEventReference

So, I use:
btnCreate.Attributes.Add("onclick", "this.value='Please wait...';this.disabled= true;" + ClientScript.GetPostBackEventReference(new PostBackOptions(this, "btnCreate")));
You are not authorized to post a reply.
Forums > PowerShell Development > Hosting the Shell > Powershell via ASPX - Pipeline problem



ActiveForums 3.7
right
   
footer Sponsored by Quest Software • SAPIEN Technologies • ShellTools, LLC • Microsoft Windows Server 2008 footer
footer