Build dynamic Web content with PowerShell
Build dynamic Web content with PowerShell
  • Classic ASP-like template language makes it easy to build dynamic Web pages using PowerShell.
  • Develop powerful yet easy to maintain Web pages based on traditional HTML/XML markup and inline PowerShell code.
Download Free Trial

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

Using PowerShell ASP

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

  1. Add a reference to the PowerShell ASP assembly: nsoftware.PowerShellASP.dll
  2. Add an entry in your Web.config file mapping the *.ps1x extension to the PowerShell ASP assembly, like this:
    <httpHandlers>
    	<add verb="*"
    		path="*.ps1x"
    		type="nsoftware.PowerShellASP.PSHandler, 
    			  nsoftware.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 PowerShell ASP pages in your Web Application.

Authoring PowerShell ASP Pages

PowerShell ASP 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.