Extend PowerShell Strings so they can read RSS Feeds(zz)

Extension Method Definition

Save the Xml to a file called GetRssMethod.ps1xml. Then load the file and update the type system definitions:

Update-TypeData GetRssMethod.ps1xml

            $wc = New-Object Net.WebClient
            ([xml] ($wc.DownloadString($this))).rss

If it worked, you won’t see errors or output.

Try it out

$url = "http://blogs.msdn.com/MainFeed.aspx?Type=AllBlogs"
$url.GetRss().Channel.Item | ft Creator, Title | More

I got the idea reading an extract from Groovy Recipes: Greasing the Wheels of Java. Groovy strings have a toUrl() method.

In my first attempt I built a .NET DLL with an extension method, loaded the assembly, it didn’t work, strings did not have the new method.

public static string GetRss(this string target) {}

I Googled ‘extension methods PowerShell’, found Harry Pierson’s post I’m Wrong Because Ruby and PowerShell Are Mainstream, that led to Scott Hansleman’s post Accessing EXIF Photo Data from JPEGs with PowerShell.

Put it all together with Windows PowerShell in Action by Bruce Payette.


posted @ 2009-03-09 00:09  stu_acer  阅读(339)  评论(0编辑  收藏