PowerShell  RSS 

PowerShellRSS 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 PowerShellRSS scripts in a PowerShell pipeline.

Using PowerShellRSS

To use PowerShellRSS 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 PowerShellToys.PowerShellRSS.dll assembly, or copy it to your web site's ./bin folder
  3. Register the PowerShellRSS ASP.NET Http Handler in your site's Web.Config file:
  4. 									<httpHandlers>
    										<add verb='GET' path='*.rs1x' 
    											type='PowerShellToys.PowerShellRSS.PSRSSHandler, PowerShellToys.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 PowerShellRSS Scripts

PowerShellRSS Scripts are regular PowerShell script files saved with the .rs1x extension. When the scripts are executed by PowerShellRSS, 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 <value> 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, PowerShellRSS will provide some basic information about your script on the generated feed metadata. 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

PowerShellRSS 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

PowerShellRSS scripts can interact with the HTTP runtime as well through the use of the ASP.NET Intrinsic objects like HttpRequest and HttpResponse. Because of the threading model used by PowerShell these 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.