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.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 ;)