PowerShell  Panel 

PowerShellPanel is an ASP.NET control that can render PowerShellASP templates from within any ASP.NET web Page. PowerShellASP is an ASP-like template language that contains a mixture of markup (HTML, XML or whatever you want to generate) and inline PowerShell code.

Using PowerShellPanel ASP.NET Control

To use PowerShellPanel all you need to do is follow these simple steps:

  1. Create a new ASP.NET Web Site/Application or open an existing one in Visual Studio.
  2. Add a reference to the PowerShellToys.PowerShellPanel.dll assembly, or copy it to your web site's ./bin folder
  3. In the ASP.NET page or control you want to use PowerShellPanel, register the control:
    									<%@ Register Assembly="PowerShellToys.PowerShellPanel"
    										Namespace="PowerShellToys.PowerShellPanel"
    										TagPrefix="cc1" %>
    									
  4. Add the PowerShellPanel control and simply write your PowerShellASP script directly into the panel:
    									<cc1:PowerShellPanel ID="PSPanel2" runat="server">
    									<asp:Label ID='Label2' ForeColor="Red" runat="server" Text="<%= $request.PhysicalApplicationPath %>" />
    									<ol>
    										<% ls 'c:\' | %{ %>
    											<li><%= $_.Name %></li>
    										<% } %>
    									</ol>
    									</cc1:PowerShellPanel>
    									

Using Alternate Syntax

To avoid excessive code escaping when writing your PowerShellASP set the UseAlternateDelimiters property of the PowerShellPanel control to true.

By setting this to true, PowerShellPanel will use the [% and %] sequences as delimiters for code blocks instead of the default <% and %> delimiters. The example above could be written like this instead:

								<cc1:PowerShellPanel ID="PSPanel1" runat="server" 
									UseAlternateDelimiters='true'>
								<asp:Label ID='Label1' ForeColor="Red" runat="server" 
									Text="[%= $request.PhysicalApplicationPath %]" />
								<ol>
									[% ls 'c:\' | %{ %]
									<li>[%= $_.Name %]</li>
									[% } %]
								</ol>
								</cc1:PowerShellPanel>
								

Intrinsic Objects

Besides running standard PowerShell code, you will want to interact with the HTTP runtime through the use of the ASP.NET Intrinsic objects like HttpRequest and HttpResponse. Because of the threading model used by PowerShell, those objects aren’t accessible directly through HttpContext.Runtime.

  • $Request: Contains the HttpRequest object.
  • $Server: Contains the HttpUtility object.
  • $Session: Contains the HttpSession object.
  • $Application: Contains the HttpApplication object.
  • $Response: Contains the HttpResponse object. You can write directly to the response stream from code if you want, or you can use write-host and friends as well.
  • $Cache: Contains the HttpCache object.
  • $Context: The HttpContext object associated with the current request.

Using these objects works exactly the same as in regular ASP.NET applications. The following script will dump all of the values in the HttpRequest object to a simple HTML page:

    <% $request.params | %{
	    write "$_ = $($request[$_])<br/>"
	} %>

As you can see, this sample uses less of the templating capabilities and instead uses simple PowerShell expressions to generate the output.