Microcode: Scripting RSS Feeds with PowerShell and Microsoft.FeedsManager

PowerShell's an amazing glue language.   It can help you bring code from all corners of the earth into one environment, and then you can custom the code to be more to your liking.  While the last several entries of my blog have spent time looking at what we can glue together with .NET, this one shows you how you can bring COM into the mix.

Internet Explorer has an RSS feed manager which you can script in .NET.  While the command line might not be the ideal spot to read your blogs, it's possible to make a much nicer front end by scripting WPF & PowerShell, and having a quick and easy API to get at your RSS feeds makes building applications that use RSS feeds as a way to synchronize information between multiple machines.

The object that makes this all work is Microsoft.FeedsManager, and the cmdlet that makes working with it possible is New-Object.  By default, New-Object will create a new .NET object from the classes that are currently loaded by .NET (for more on this, see my previous posts about Get-Type and Get-Assembly).  While .NET is a fairly easy world to explore, COM is considerably more tricky.

Luckily, COM objects are the main backbone of VBScript.  This is an important point for VBScripters trying to learn PowerShell: while the world of .NET may be new and exotic, and Cmdlets might be cool, you don't have to drop your VBScript knowledge at the door.  While some of a VBScript (e.g. strings) will require new learning in PowerShell, the vast majority of it doesn't.

Microsoft.FeedsManager is one of these examples.  I found out about this object by looking at the source code for the RSS reader vista sidebar gadget, and, once I knew its name, I knew how to get at it in PowerShell.  Even though I was looking at javascript, I knew that there was a scriptable COM object there to help me out, and I knew how to bring it into PowerShell.

To create a Microsoft.FeedsMananger, simply use this one liner:

New-Object -comObject Microsoft.FeedsManager

Since the root folder looks promising, let's explore that:

(New-Object -comObject Microsoft.FeedsManager).RootFolder

While I can just save this into a variable and script it like I were still in VBScript land (e.g. $feeds = New-Object -comObject Microsoft.FeedsManager, $feeds.RootFolder), I far prefer the parlance of PowerShell, so I'll make a quick Get-Feed function to explore feeds more easily.  I'll accept a wildcard for the name or title of the feed, and, since the feeds can have an arbitrary number of subfolders, I'll add a -recurse parameter.  In order to make -recurse work elegantly, I'll use a short recursive script and I'll add a $folder parameter, which can be null and will be assumed to be the root folder.  Finally, since I'll often want to read the items more than the feed, I'll add a switch parameter to extract the articles.

function Get-Feed($feed = "*", $folder, [switch]$recurse, [switch]$articles) {
if (! $folder) {
$feedsManager = New-Object -ComObject Microsoft.FeedsManager
$folder = $feedsManager.RootFolder
}
$folder.Feeds |
Where-Object {
($_.Title -like "$feed") -or ($_.Name -like "$feed")
} | Foreach-Object {
if ($articles) { $_.Items } else { $_ }
}
if ($recurse) {
$folder.Subfolders | Foreach-Object {
Get-Feed $feed $_ -recurse -articles:$articles
}
}
}

Once I've defined Get-Feed, I can use it to search my feeds through PowerShell.  This one liner will get my all my feeds, sort them by name, and display just the Name and URL:

Get-Feed -recurse | Sort-Object Name | Select-Object Name, URL

To see all of the feed items sort them by when they were published, and display the post title, the blog title, and the publish date, you can use this pipeline:

Get-Feed -articles -recurse |
Sort-Object PubDate -descending |
Select-Object Title, @{Name='Blog'; Expression={$_.Parent.Title }}, PubDate

I promise that we can do much more with RSS and PowerShell, but hopefully this post will get you started down the path of using the FeedsManager from PowerShell.

Hope this helps,

James Brundage [MSFT]

Module Name

Scripts

RSS

Get-Feed