PowerShell: Parse an RSS Feed and E-mail/Text Yourself the Results

I’m so excited — my best friend and tech partner-in-crime, Brandon, recently picked up PowerShell after I gave him a copy of Wrox’s Professional PowerShell. He totally loves it and has already created a few new PowerShell scripts and converted other ASP.NET/VBScript scripts. One of his favorite scripts checks the Woot.com RSS feed and e-mails him with the daily item. He reduced a 25 line VBScript to a 5 line PowerShell script which goes something like this:

$rssUrl = "http://www.woot.com/blog/rss.aspx"
$blog = [xml](new-object System.Net.WebClient).DownloadString($rssUrl)
$results = $blog.rss.channel.item[0].title

$smtpmail = [System.Net.Mail.SMTPClient]("smtp.san.rr.com")
$smtpmail.Send("[email protected]", "[email protected] ", "Woot Deal", $results)

Five silly lines! PowerShell is just so straightforward:

1) Assign URL to a variable
2) Create a new WebClient and pull the RSS Feed into it.
3) Assign the value of the first title element to a variable
4) Create a SMTP Client and assign it the value of the smtp host
5) Access the Send Method and pass it (From, To, Subject, Body)

He then used Task Scheduler (tasksch) to run the script each night.

Another script he wrote checks for backups older than seven days then deletes them. If you use this script, be sure to remove the -whatif if you are happy with the results.

foreach ($file in (get-childitem -Path C:\backups\))
{
  $curTime = [System.DateTime]::get_now()
  if ($file.LastWriteTime -lt $curTime.Add("-7"))
   {
    remove-item -recurse $file -whatif
   }
}

For the record, I have Professional Windows PowerShell (Wrox/Andrew Watt), Windows PowerShell Cookbook (O’reilly/Lee Holmes, MSFT) and PowerShell in Action (Manning/Bruce Payette, MSFT). I suggest reading all three of them, in that order. The Wrox and O’reilly books provide the “how” while Payette’s book provides the “why?”. All three are very well written, especially the Cookbook, for which I was a technical editor ;)

Chrissy is a PowerShell MVP who has worked in IT for nearly 20 years, and currently serves as a Sr. Database Engineer in Belgium. Always an avid scripter, she attended the Monad session at Microsoft’s Professional Developers Conference in Los Angeles back in 2005 and has worked and played with PowerShell ever since. Chrissy is currently pursuing an MS in Systems Engineering at Regis University and helps maintain RealCajunRecipes.com in her spare time. She holds a number of certifications, including those relating to SQL Server, SuSE Linux, SharePoint and network security. She recently became co-lead of the SQL PASS PowerShell Virtual Chapter. You can follow her on Twitter at @cl.

Posted in PowerShell
5 comments on “PowerShell: Parse an RSS Feed and E-mail/Text Yourself the Results
  1. Corey says:

    Oh, that is sweet.

    I just had to write some WSH for the first time in years and now I realize I should have taken a day or two and learned enough PowerShell to pull off the same moves.

    For the interested, my scripts take uncompressed audio files and uploads them to S3 — 250 GB in the first batch. It is quite a rube goldberg though since the S3 upload tool I picked is a Java based “shell”, not an object I can interact with directly.

  2. Bruno Gomes says:

    Hi there!
    If you want to delete files older than seven days, shouldn’t you say:
    if ($file.LastWriteTime -lt $curTime.Add(“-7”))

    instead of
    if ($file.LastWriteTime -gt $curTime.Add(“-7”))
    ?

  3. Chrissy says:

    Hey Corey,
    I think it’s important to know both.. practicing your WSH will likely come in handy at some point.

    That sounds like a tough script.. how long did it take to pull off?

  4. Chrissy says:

    Hey Bruno,
    I didn’t try the script but I believe if you did less than -7, it would delete everything from the present time until 1 week ago. He’s looking to delete everything with a timestamp greater than 1 week ago.

  5. Brandon says:

    I want to mention an issue I discovered while trying to delete folders with this PowerShell script…
    If you have a folder containing a Thumbs.db file, the folder will not delete.

    I think Bruno is right about the -lt vs. -gt. I updated the post.

1 Pings/Trackbacks for "PowerShell: Parse an RSS Feed and E-mail/Text Yourself the Results"
  1. […] Thanks goes out to Chrissy over at NetNerds for the inspiration. […]

Leave a Reply

Your email address will not be published. Required fields are marked *

*