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.
  • FREE 'Community License'.
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.

NEW PowerShell ASP V3 - Includes powerful updates and performance enhancements, and best of all, includes a new personal edition that is completely FREE when accessed from a single concurrent connection! Download the latest PowerShell ASP V3 release, and start building dynamic Web content with PowerShell today!

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 is no 'code behind' model for PS1X pages; in this sense they resemble more the ASP classic model.

Here is 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 is 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>


 

Dynamic RSS and Atom Feeds

PowerShell ASP allows you to generate and serve RSS and Atom feeds from PowerShell scripts executed on an ASP.NET Web server. Feeds are generated automatically based on the objects returned by the execution of special PowerShell RSS scripts in a PowerShell pipeline.

Using PowerShell ASP for Feed Creation

To enable PowerShell RSS scripts all you need to do is follow these simple steps:

  1. Create a new ASP.NET Web Site/Application
  2. Add a reference to the nsoftware.PowerShellRSS.dll assembly, or copy it to your website's ./bin folder
  3. Register the PowerShell RSS ASP.NET Http Handler in your site's Web.Config file:
  4. <httpHandlers>
    	<add verb='GET' path='*.rs1x' 
    		type='nsoftware.PowerShellRSS.PSRSSHandler, nsoftware.PowerShellRSS'/>
    </httpHandlers>
  5. If you have not done so before, configure the *.rs1x extension to be mapped to the ASP.NET ISAPI extension library in your IIS application.

Creating PowerShell RSS Scripts

PowerShell RSS Scripts are regular PowerShell script files saved with the .rs1x extension. When the scripts are executed by PowerShell ASP, the objects returned to the pipeline are automatically converted to RSS/Atom feed items based on these rules:

  • Each object returned by the pipeline becomes one RSS item.
  • If the object is just a regular, primitive value, like a string or number, its value is used as the title of the RSS entry.
  • If the object is an array, then each item in the array is written as a element in the RSS entry
  • If the object is a hashtable, then each key/value pair in it is written as an element in the RSS entry, using the key as the element name.

For example, the following sample script generates 3 RSS entries:

# a simple value translates into an item
"This is a simple Item"

# an array translates into a single item (but no way to set the title)
,('an', 'array')

# a hashtable allows you to set your own element names
@{ 'name' = 'John'; Value = 'Foo' }

Customizing RSS Generation

If you need finer control over how the objects are translated into RSS entries you can return a hashtable instead. Hashtables allow fine control over the names of the elements in the RSS feed entry and also allow you to override valuesfor basic RSS entry properties by prefixing keys with 'rss:' or 'atom:'.

Here's an example that generates a feed from a list of files on the C: drive, and customizes how the entries are translated:

ls c:\ | %{
   @{
	  'rss:id' = "urn:$($_.Name)";
	  'rss:updated' = $_.LastWriteTime.ToString('R');
	  'rss:title' = $_.Name;
	  'fullname'  = $_.FullName;
	  'size' = $_.Length;
	  'directory' = $_.PSIsContainer;
	  'Mode' = $_.Mode;
   }
}

Overriding Feed Attributes

By default, PowerShell ASP will provide some basic information about your script on the generated feed meta data. For example the title of the feed will be the file name of your *.rs1x script.

However, you can customize the values of these attributes by calling the Set-FeedAttr function from within script. The only arguments needed are the name of the attribute and its value.

set-feedattr 'title' "This is a sample feed"
										
set-feedattr 'link' "http://my.feed.url.here/"

Generating Atom Feeds

PowerShell ASP will generate RSS feeds by default. However, if you add "?@atom" to the Query String in the feed URL PowerShellRSS will generate the feed using the Atom specification instead.



 

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.