PowerShell ASP: Automatically Convert PSObjects to RSS Items

In the same way PowerShell ASP can be used to generate dynamic Web content, the PowerShell RSS component (included in PowerShell ASP) can be used to generate dynamic RSS feeds. PowerShell RSS provides the easiest way to create dynamic RSS feeds from PowerShell scripts.

The Get-ChildItem command in PowerShell is used to get a listing of files to use as enclosures in the feed.

<%
# This example demonstrates how to generate an RSS Feed from a call to the Get-ChildItem cmdlet (dir). # To tailor to your specific needs, set the following values: $mediadir = 'C:\Testing\media';
$virtualdirectory = 'media';

# Check for user input $dir = $request['path']
if ( $dir -eq $null ) {
   $dir = $mediadir;
}

#Set the feed title and other basic feed attributes set-feedattr 'rss:title' "PowerShellASP Get-RSS Sample";
set-feedattr 'rss:link' "http://www.nsoftware.com";
set-feedattr 'rss:description' "This example uses PowerShellASP to generate an RSS feed using PowerShell cmdlets.";

ls $dir | %{
   @{
      'rss:updated' = $_.LastWriteTime.ToString('R');
      'rss:title' = "Podcast episode: $($_.Name)";
      'rss:enclosure@url' = "http://" + $request.servervariables["SERVER_NAME"] + ":" + $request.servervariables["SERVER_PORT"] + "/$virtualdirectory/" + $_.Name; 
      'rss:enclosure@length' = $_.Length;
      'rss:enclosure@type' = "audio/mp3";
   }
}
%>

That is all there is to it. A couple of lines of script produces an RSS formatted feed of data produced by PowerShell.


We appreciate your feedback.  If you have any questions, comments, or suggestions about this article please contact our support team at kb@nsoftware.com.