header1   header
header
header Register : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
Powershell via ASPX - Pipeline problem
Last Post 03 Dec 2007 11:05 AM by Karl Mitschke. 4 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev
You are not authorized to post a reply.
Author Messages
Karl MitschkeUser is Offline
Basic Member
Basic Member
Posts:457
Avatar

--
28 Nov 2007 09: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/Hosti...SPNET.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 ;)

    http://unlockpowershell.wordpress.com
    Co-Author, Windows PowerShell 2.0 Bible
    -join("6B61726C6D69747363686B65406D742E6E6574"-split"(?<=\G.{2})",19|%{[char][int]"0x$_"})
    Marco ShawUser is Offline
    Veteran Member
    Veteran Member
    Posts:1684
    Avatar

    --
    29 Nov 2007 09:55 AM
    Karl MitschkeUser is Offline
    Basic Member
    Basic Member
    Posts:457
    Avatar

    --
    30 Nov 2007 08: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/Hosti...SPNET.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.
    http://unlockpowershell.wordpress.com
    Co-Author, Windows PowerShell 2.0 Bible
    -join("6B61726C6D69747363686B65406D742E6E6574"-split"(?<=\G.{2})",19|%{[char][int]"0x$_"})
    Karl MitschkeUser is Offline
    Basic Member
    Basic Member
    Posts:457
    Avatar

    --
    30 Nov 2007 09: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);

    http://unlockpowershell.wordpress.com
    Co-Author, Windows PowerShell 2.0 Bible
    -join("6B61726C6D69747363686B65406D742E6E6574"-split"(?<=\G.{2})",19|%{[char][int]"0x$_"})
    Karl MitschkeUser is Offline
    Basic Member
    Basic Member
    Posts:457
    Avatar

    --
    03 Dec 2007 11:05 AM
    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")));
    http://unlockpowershell.wordpress.com
    Co-Author, Windows PowerShell 2.0 Bible
    -join("6B61726C6D69747363686B65406D742E6E6574"-split"(?<=\G.{2})",19|%{[char][int]"0x$_"})
    You are not authorized to post a reply.


    Active Forums 4.3
    right
    footer   footer
    footer Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 R2 footer
    footer   footer