The PowerShell Iron Scripter: My solution to prequel puzzle 3

I’ve been working through the Iron Scripter 2018 prequel puzzles which can be found on PowerShell.org’s website. In puzzle 3, you’re asked to create a “reusable PowerShell artifact”. To me, that almost always means a PowerShell function. One requirement is to pull information from the PowerShell.org RSS feed. Invoke-RestMethod which was introduced in PowerShell version 3.0 is the easiest way to accomplish that task.

You’re also asked to display the returned information in a way that allows the user to select an item from the feed. My first thought was to use Out-GridView to build a GUI based menu for selecting the item, but that won’t work in PowerShell Core since it doesn’t have the same GUI based commands as Windows PowerShell. This means I’ll have to build a text based menu in PowerShell. Luckily, I’ve recently read “The PowerShell Scripting and Toolmaking Book” by Don Jones and Jeff Hicks which has some examples of building text based menus, although I designed mine a little differently than theirs. Consuming content from others often sparks your own creativity.

The next part was actually the most challenging. Give the user the ability to display the selected item from the RSS feed in either their browser or in plain text.

While the browser part was easy enough and the text part was also easy for Windows PowerShell, returning the content of the feed in text for PowerShell Core was more difficult. This was due to the ParsedHtml property not existing for Invoke-WebRequest in PowerShell Core.

One of the drawbacks to using Invoke-WebRequest in Windows PowerShell is that it’s dependent on Internet Explorer, although that isn’t the case in PowerShell Core running on a Windows system.

Making it work for any RSS feed was a bonus and mine does work against a couple of different sites I tested it against (PowerShell.org and my mikefrobbins.com blog site). Your results may vary when using it for RSS feeds on other websites.

I use a regular expression to parse the text if PowerShell Core is used. That regular expression could probably use some more tweaking and I’ll post an update if I decide to work on it more. The other things that could be added are error handling and verbose output.

One of the rules I’ve broken in the PowerShell function shown in this blog article is that a function should do one thing and do it well. More about that rule can be read about in this Hey, Scripting Guy! Blog article. This function could be broken up into multiple functions to better adhere to that rule.

The function shown in this blog article can be downloaded from my IronScripter repository on GitHub. It can also be installed from the PowerShell Gallery.

See the scripting games category on this blog site to view my other Iron Scripter entries along with my previous scripting games entries. Other recommended reading: PowerShell Core 6.1 Web Cmdlets Roadmap blog article by Microsoft MVP Mark Kraus.

Learn to write award winning PowerShell functions and script modules from a former winner of the advanced category in the scripting games and a multiyear recipient of both Microsoft’s MVP and SAPIEN Technologies’ MVP award at the PowerShell + DevOps Global Summit 2018.


Update – February 4th, 2018
I modified the switch statement to use the same code regardless if Windows PowerShell or PowerShell Core is used.

µ

1 Comment

  1. Jeff Jerousek (@jeffjerousek)

    I believe you can use the parameter -UseBasicParsing to avoid using IE starting in PowerShell 5.0.

    Reply

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: