Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to explore an RSS feed from the Hey, Scripting Guy! Blog.
Microsoft Scripting Guy, Ed Wilson, is here. It is just a few days until I speak at the Central Ohio Windows PowerShell User Group. The Scripting Wife and I will be there on October 2, 2012. For registration information and a précis of my talk, see the Central Ohio PowerShell Users Group site.
On the weekend, I like to play around with Windows PowerShell. I also like to experiment with new tea combinations. This morning I am sipping a nice Monkey Picked Oolong tea with little jasmine buds, lemon grass, and hibiscus flower. I sweetened the tea with a little bit of crushed cinnamon stick. Yum!
I ran across an old blog post the other day that was written by a guy who was really pleased that Windows PowerShell enabled him to reduce a 25-line script to five lines to retrieve RSS feeds. In Windows PowerShell 3.0, that is one line. Here is the line to retrieve an RSS feed from the Hey, Scripting Guy! Blog.
[xml]$hsg = Invoke-WebRequest http://blogs.technet.com/b/heyscriptingguy/atom.aspx
The important thing here is to ensure that I use the [xml] type accelerator to convert the returned information into an XMLDocument object. After I have the returned information, I view it in the Windows PowerShell console. I see there is a Feed property as shown here.
PS C:\> [xml]$hsg = Invoke-WebRequest http://blogs.technet.com/b/heyscriptingguy/atom
.aspx
PS C:\> $hsg
xml xml-stylesheet feed
--- -------------- ----
version="1.0" encoding="U... type="text/xsl" href="ht... feed
When I access the Feed property, I see a number of interesting items, the most important of which is the Entry property. These properties are shown here.
PS C:\> $hsg.feed
xmlns : http://www.w3.org/2005/Atom
lang : en-US
title : title
subtitle : subtitle
id : http://blogs.technet.com/b/heyscriptingguy/atom.aspx
link : {link, link}
generator : generator
updated : 2012-09-09T00:01:00Z
entry : {entry, entry, entry, entry...}
This is where Windows PowerShell 3.0 really comes in handy. I can directly access the collection of entries by using dotted notation. It is cool and powerful. In the following example, I show only the first record.
PS C:\> $hsg.feed.entry
title : PowerTip: Redirect PowerShell Warning Messages to a Text File
link : link
id : http://blogs.technet.com/b/heyscriptingguy/archive/2012/09/20/powertip-r
edirect-powershell-warning-messages-to-a-text-file.aspx
published : 2012-09-20T18:59:00Z
updated : 2012-09-20T18:59:00Z
content : content
author : author
category : {category, category, category, category}
Of interest to me are the blog posts with their publication dates. I can easily retrieve a list of blog titles and dates as shown here.
PS C:\> $hsg.feed.entry | select title, published
title published
----- ---------
PowerTip: Redirect PowerShell Warning M... 2012-09-20T18:59:00Z
Use PowerShell Redirection Operators fo... 2012-09-20T07:01:00Z
PowerTip: Display PowerShell Informatio... 2012-09-19T18:59:00Z
Use PowerShell to Create Intelligent De... 2012-09-19T07:01:00Z
PowerTip: Use Show-Command to See Power... 2012-09-18T18:59:00Z
Create a PowerShell Scheduled Job 2012-09-18T07:01:00Z
PowerTip: Examine Automatic Variables 2012-09-17T18:59:00Z
PowerShell and User Access Logging 2012-09-17T07:01:00Z
PowerTip: Easily Find the PowerShell 3.... 2012-09-16T18:59:00Z
Weekend Scripter: Get Detailed PowerShe... 2012-09-16T07:01:00Z
PowerTip: Quickly Find System Updates v... 2012-09-15T18:59:00Z
Weekend Scripter: What Does a PowerShel... 2012-09-15T07:01:00Z
PowerTip: Where Did that PowerShell Cmd... 2012-09-14T18:59:00Z
Easily Unblock All Files in a Directory... 2012-09-14T07:01:00Z
PowerTip: Use PowerShell to Easily Read... 2012-09-13T18:59:00Z
Use PowerShell to Simplify Access to XM... 2012-09-13T07:01:00Z
PowerTip: Easily Find WMI Class Schema ... 2012-09-12T18:59:00Z
Use PowerShell to Troubleshoot ???Provi... 2012-09-12T07:01:00Z
The Scripting Guy Talks About PowerShel... 2012-09-11T19:41:00Z
PowerTip: Discovering PowerShell Functions 2012-09-11T18:59:00Z
Bespoke Scripting? What Do You Want in ... 2012-09-11T07:01:00Z
PowerTip: Working with the Maximum Hist... 2012-09-10T18:59:00Z
Increase PowerShell Command History to ... 2012-09-10T07:01:00Z
PowerTip: Two Quick Ways to Check Varia... 2012-09-09T18:59:00Z
Weekend Scripter: PowerShell Saturday S... 2012-09-09T07:01:00Z
One problem with the previous output is that the dates and times are a bit difficult for me to understand. I use a hash table to change the display of the date as shown here.
PS C:\> $hsg.feed.entry | select title, @{LABEL="Published"; EXPRESSION={[datetime]$_
.Published} }
title Published
----- ---------
PowerTip: Redirect PowerShell Warning M... 9/20/2012 2:59:00 PM
Use PowerShell Redirection Operators fo... 9/20/2012 3:01:00 AM
PowerTip: Display PowerShell Informatio... 9/19/2012 2:59:00 PM
Use PowerShell to Create Intelligent De... 9/19/2012 3:01:00 AM
PowerTip: Use Show-Command to See Power... 9/18/2012 2:59:00 PM
Create a PowerShell Scheduled Job 9/18/2012 3:01:00 AM
PowerTip: Examine Automatic Variables 9/17/2012 2:59:00 PM
PowerShell and User Access Logging 9/17/2012 3:01:00 AM
PowerTip: Easily Find the PowerShell 3.... 9/16/2012 2:59:00 PM
Weekend Scripter: Get Detailed PowerShe... 9/16/2012 3:01:00 AM
PowerTip: Quickly Find System Updates v... 9/15/2012 2:59:00 PM
Weekend Scripter: What Does a PowerShel... 9/15/2012 3:01:00 AM
PowerTip: Where Did that PowerShell Cmd... 9/14/2012 2:59:00 PM
Easily Unblock All Files in a Directory... 9/14/2012 3:01:00 AM
PowerTip: Use PowerShell to Easily Read... 9/13/2012 2:59:00 PM
Use PowerShell to Simplify Access to XM... 9/13/2012 3:01:00 AM
PowerTip: Easily Find WMI Class Schema ... 9/12/2012 2:59:00 PM
Use PowerShell to Troubleshoot ???Provi... 9/12/2012 3:01:00 AM
The Scripting Guy Talks About PowerShel... 9/11/2012 3:41:00 PM
PowerTip: Discovering PowerShell Functions 9/11/2012 2:59:00 PM
Bespoke Scripting? What Do You Want in ... 9/11/2012 3:01:00 AM
PowerTip: Working with the Maximum Hist... 9/10/2012 2:59:00 PM
Increase PowerShell Command History to ... 9/10/2012 3:01:00 AM
PowerTip: Two Quick Ways to Check Varia... 9/9/2012 2:59:00 PM
Weekend Scripter: PowerShell Saturday S... 9/9/2012 3:01:00 AM
Now that I have a DateTime object instead of a simple string, I can do other things, such as show the morning and afternoon postings of the Hey Scripting Guy! Blog.
PS C:\> $hsg.feed.entry | select title, @{LABEL="Published"; EXPRESSION={[datetime]$_
.Published} } | select title, @{LABEL="Morning/Afternoon"; EXPRESSION={if($_.publishe
d.timeofday.hours -gt 12) {"Afternoon"} ELSE {"Morning"}} } | sort 'morning/afternoon
'
title Morning/Afternoon
----- -----------------
PowerTip: Redirect PowerShell Warning M... Afternoon
PowerTip: Working with the Maximum Hist... Afternoon
PowerTip: Discovering PowerShell Functions Afternoon
The Scripting Guy Talks About PowerShel... Afternoon
PowerTip: Easily Find WMI Class Schema ... Afternoon
PowerTip: Use PowerShell to Easily Read... Afternoon
PowerTip: Two Quick Ways to Check Varia... Afternoon
PowerTip: Quickly Find System Updates v... Afternoon
PowerTip: Where Did that PowerShell Cmd... Afternoon
PowerTip: Examine Automatic Variables Afternoon
PowerTip: Use Show-Command to See Power... Afternoon
PowerTip: Display PowerShell Informatio... Afternoon
PowerTip: Easily Find the PowerShell 3.... Afternoon
Weekend Scripter: Get Detailed PowerShe... Morning
PowerShell and User Access Logging Morning
Weekend Scripter: What Does a PowerShel... Morning
Easily Unblock All Files in a Directory... Morning
Create a PowerShell Scheduled Job Morning
Use PowerShell to Simplify Access to XM... Morning
Use PowerShell to Troubleshoot ???Provi... Morning
Use PowerShell to Create Intelligent De... Morning
Bespoke Scripting? What Do You Want in ... Morning
Use PowerShell Redirection Operators fo... Morning
Increase PowerShell Command History to ... Morning
Weekend Scripter: PowerShell Saturday S... Morning
Another cool thing that I can do is check the tags on my blogs. To do this, I use the Category property from the entry object. I group based on the terms of the categories, and then sort the list. This command is shown here.
$hsg.feed.entry.category | group term -NoElement | sort count –Descending
The command and associated output are shown in the image that follows.
That is about it for today—I need to get to work on my new Windows PowerShell book project.
I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy