PowerShell  ASP 

PowerShellASP is an ASP-like template language for Web Applications; templates contain a mixture of markup (HTML, XML or whatever you want to generate) and inline PowerShell code. At runtime, templates/pages are fully translated to PowerShell code and executed as a single unit inside a PowerShell pipeline, with the results sent to the client browser.

PowerShellASP runs on the ASP.NET platform, implemented as a custom IHttpHandler mapped to *.ps1x files. Because of this, you can mix PowerShellASP pages alongside any ASP.NET application. This provides a great way to leverage PowerShellASP inside your existing applications as needed or you can create complete applications from scratch based only on *.ps1x pages.

Using PowerShellASP

You can use PowerShellASP in any regular ASP.NET application project by following these simple steps:

  1. Add a reference to the PowerShellASP assembly: PowerShellToys.PowerShellASP.dll
  2. Add an entry in your Web.config file mapping the *.ps1x extension to the PowerShellASP assembly, like this:
    									<httpHandlers>
    										<add verb="*"
    											path="*.ps1x"
    											type="PowerShellToys.PowerShellASP.PSHandler, 
    												  PowerShellToys.PowerShellASP"
    										/>
    									</httpHandlers>
    								
  3. If you have not done so before, configure the *.ps1x extension to be mapped to the ASP.NET ISAPI extension library in your IIS application.

 After following these simple steps you’ll be ready to create PowerShellASP pages in your Web Application.



Authoring PowerShellASP Pages

PowerShellASP pages are simple text files with the *.ps1x extension that contain both markup as well as snippets of regular PowerShell code interacting together. Unlike ASP.NET, there’s no “code behind” model for PS1X pages; in this sense they resemble more the ASP classic model.

Here’s a very simple PS1X page:

									<html>
										<body>
											<h1>
												Hello <%= $request['name'] %>! 
											</h1>									
										</body>
									</html>
								

As you can see, everything is HTML markup right until the <%= %> section, which means “evaluate this PowerShell expression and print the result”. The expression, in this case, is using the intrinsic ASP.NET Request object to query data coming in the query string of the URL.

You can also create full code blocks that include any other kind of PowerShell expression or flow control construct, and even intermingle that with markup code. For example, here’s a simple page that will present the list of running processes on the machine:

									<html>
										<body>
											<table>
											<tr><td>ID</td><td>Name</td></tr>
											<% get-process | %{ %>
												<tr>
													<td><%=$_.Id%></td>
													<td><%=$_.ProcessName%></td>
												</tr>
												<% } %>
											</table>
										</body>
									</html>
								

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.