Skip to content
Menu
The Lonely Administrator
  • PowerShell Tips & Tricks
  • Books & Training
  • Essential PowerShell Learning Resources
  • Privacy Policy
  • About Me
The Lonely Administrator

Friday Fun: Quote of the Day Revised

Posted on June 28, 2013

talkbubble-v3This 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.

Manage and Report Active Directory, Exchange and Microsoft 365 with
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.


Behind the PowerShell Pipeline

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Mastodon (Opens in new window) Mastodon
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to print (Opens in new window) Print
  • Click to email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

6 thoughts on “Friday Fun: Quote of the Day Revised”

  1. Pingback: Microsoft Most Valuable Professional (MVP) – Best Posts of the Week around Windows Server, Exchange, SystemCenter and more – #35 - TechCenter - Blog - TechCenter – Dell Community
  2. RPA says:
    July 1, 2013 at 8:37 am

    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

    1. Jeffery Hicks says:
      July 1, 2013 at 9:25 am

      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.

  3. Pingback: Week of July 1: New blogs from Windows Server/System Center MVPs - Server and Cloud Partner and Customer Solutions Team Blog - Site Home - TechNet Blogs
  4. Pingback: Automating Microsoft Lync using Windows PowerShell | blog.powershell.no
  5. Pingback: Automating Microsoft Lync using Windows PowerShell - Jan Egil`s blog on Microsoft Infrastructure

Comments are closed.

reports

Powered by Buttondown.

Join me on Mastodon

The PowerShell Practice Primer
Learn PowerShell in a Month of Lunches Fourth edition


Get More PowerShell Books

Other Online Content

github



PluralSightAuthor

Active Directory ADSI Automation Backup Books CIM CLI conferences console Friday Fun FridayFun Function functions Get-WMIObject GitHub hashtable HTML Hyper-V Iron Scripter ISE Measure-Object module modules MrRoboto new-object objects Out-Gridview Pipeline PowerShell PowerShell ISE Profile prompt Registry Regular Expressions remoting SAPIEN ScriptBlock Scripting Techmentor Training VBScript WMI WPF Write-Host xml

©2025 The Lonely Administrator | Powered by SuperbThemes!
%d