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

<Types>
  <Type>
    <Name>System.String</Name>
      <Members>
        <ScriptMethod>
          <Name>GetRss</Name>
          <Script>
            $wc = New-Object Net.WebClient
            ([xml] ($wc.DownloadString($this))).rss
          </Script>
        </ScriptMethod>
      </Members>
  </Type>
</Types>

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
image 

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.

原文地址:http://dougfinke.com/blog/index.php/2008/01/13/extend-powershell-strings-so-they-can-read-rss-feeds/

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