This week TrainSignal has been running a contest to celebrate my new PowerShell 3.0 course . All you have to do to win is enter some off-the-wall, silly or non-production use of PowerShell. I've posted a few examples on the TrainSignal blog this week. These Friday Fun posts I write also follow the same idea. Although, I do have a sneaky intention of teaching you something about PowerShell without you realizing it.
ManageEngine ADManager Plus - Download Free Trial
Exclusive offer on ADManager Plus for US and UK regions. Claim now!
For a while now I've been using a function to get the latest quote of the day from Brainyquotes. When I first wrote the function I had to resort to .NET and the webclient class. But now with PowerShell 3.0, we have new web cmdlets that are even easier to use so I decided to rewrite my function.
#requires -version 3.0 # ----------------------------------------------------------------------------- # Script: Get-QOTD.ps1 # Author: Jeffery Hicks # https://jdhitsolutions.com/blog # follow on Twitter: http://twitter.com/JeffHicks # Date: 6/28/2013 # Version: 2.0 # Keywords: RSS, XML, REST # Comments: # # "Those who neglect to script are doomed to repeat their work." # # **************************************************************** # * DO NOT USE IN A PRODUCTION ENVIRONMENT UNTIL YOU HAVE TESTED * # * THOROUGHLY IN A LAB ENVIRONMENT. USE AT YOUR OWN RISK. IF * # * YOU DO NOT UNDERSTAND WHAT THIS SCRIPT DOES OR HOW IT WORKS, * # * DO NOT USE IT OUTSIDE OF A SECURE, TEST SETTING. * # **************************************************************** # ----------------------------------------------------------------------------- Function Get-QOTD { <# .Synopsis Download quote of the day. .Description Using Invoke-RestMethod download the quote of the day from the BrainyQuote RSS feed. The URL parameter has the necessary default value. .Example PS C:\> get-qotd "We choose our joys and sorrows long before we experience them." - Khalil Gibran .Link Invoke-RestMethod #> [cmdletBinding()] Param( [Parameter(Position=0)] [ValidateNotNullorEmpty()] [string]$Url="http://feeds.feedburner.com/brainyquote/QUOTEBR" ) Write-Verbose "$(Get-Date) Starting Get-QOTD" Write-Verbose "$(Get-Date) Connecting to $url" Try { #retrieve the url using Invoke-RestMethod Write-Verbose "$(Get-Date) Running Invoke-Restmethod" #if there is an exception, store it in my own variable. $data = Invoke-RestMethod -Uri $url -ErrorAction Stop -ErrorVariable myErr #The first quote will be the most recent Write-Verbose "$(Get-Date) retrieved data" $quote = $data[0] } Catch { $msg = "There was an error connecting to $url. " $msg += "$($myErr.Message)." Write-Warning $msg } #only process if we got a valid quote response if ($quote.description) { Write-Verbose "$(Get-Date) Processing $($quote.OrigLink)" #write a quote string to the pipeline "{0} - {1}" -f $quote.Description,$quote.Title } else { Write-Warning "Failed to get expected QOTD data from $url." } Write-Verbose "$(Get-Date) Ending Get-QOTD" } #end Get-QOTD #OPTIONAL: create an alias #Set-Alias -name "qotd" -Value Get-QOTD
The function is downloading XML content from an RSS feed. I've found that using Invoke-RestMethod is a handy cmdlet for this task because it formats the data into an easy to use object. All my function does is write a string composed of different properties of the most current entry. My first post this week on the TrainSignal blog uses some of this same code.
Ok. Maybe you don't need daily inspiration but now you've seen another example of Invoke-Restmethod in action and maybe that is something you need. Enjoy and have fun out there.
Your function looks great.
I used below 1 liner in my PS2 scripts.
([xml]((new-object System.Net.WebClient).DownloadString(“http://feeds.feedburner.com/brainyquote/QUOTEBR”))).GetElementsByTagName(“item”).Item(1).description
I don’t doubt that it works. My belief is that if I’m putting something like that into a function, I only have to write the function once so I might as well make it easy to follow. Sure, you can build a function with a long one-liner like that, but if someone else had to troubleshoot or debug it, it would be much harder. Just my opinion. In any event, thank you for your interest and taking the time to post a comment.