 |
|
|
Powershell via ASPX - Pipeline problem
|
Sort:
|
|
Prev |
You are not authorized to post a reply. |
|
Karl Mitschke
 Basic Member Posts:451

 |
| 28 Nov 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 ;)
|
|
http://unlockpowershell.wordpress.com
Co-Author, Windows PowerShell 2.0 Bible
-join("6B61726C6D69747363686B65406D742E6E6574"-split"(?<=\G.{2})",19|%{[char][int]"0x$_"}) |
|
|
Marco Shaw (MVP)
 Veteran Member Posts:1642

 |
| 29 Nov 2007 10:55 AM |
|
|
|
Marco
*Microsoft MVP - Windows PowerShell
https://mvp.support.microsoft.com/profile/Marco.Shaw
*Co-Author - Sams Windows PowerShell Unleashed 2nd Edition
*Blog - http://marcoshaw.blogspot.com |
|
|
Karl Mitschke
 Basic Member Posts:451

 |
| 30 Nov 2007 09: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 Mitschke
 Basic Member Posts:451

 |
| 30 Nov 2007 10:02 AM |
|
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 Mitschke
 Basic Member Posts:451

 |
| 03 Dec 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"))); |
|
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
|
|
 |