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

Filed under: PowerShell, Quick Code — Written by Chrissy on Thursday, October 25th, 2007 @ 1:30 pm

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("me@netnerds.net", "xxxxxxxxxx@vtext.com ", "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 -gt $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 ;)

4 Comments   -
  • Comment by Corey | October 26, 2007 @ 1:07 pm

    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.

  • Comment by Bruno Gomes | October 29, 2007 @ 5:07 am

    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"))
    ?

  • Comment by Chrissy | October 30, 2007 @ 10:44 am

    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?

  • Comment by Chrissy | October 30, 2007 @ 10:46 am

    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.

Leave your comment