Skip to content

Instantly share code, notes, and snippets.

@jdhitsolutions
Created September 26, 2018 20:54
Show Gist options
  • Save jdhitsolutions/6f2892bd9eb7a60fcf8533125d265361 to your computer and use it in GitHub Desktop.
Save jdhitsolutions/6f2892bd9eb7a60fcf8533125d265361 to your computer and use it in GitHub Desktop.
A PowerShell function to get a quote of the day from BrainyQuote.com.
#requires -version 4.0
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()]
[OutputType("String")]
[Alias("qotd")]
Param()
Write-Verbose "[$(Get-Date)] Starting Get-QOTD"
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.
$params = @{
Uri = "http://feeds.feedburner.com/brainyquote/QUOTEBR"
ErrorAction = "Stop"
ErrorVariable = "myErr"
UseBasicParsing = $True
DisableKeepAlive = $True
}
write-Verbose "[$(Get-Date)] Getting latest quote from $($params.uri)."
$data = Invoke-RestMethod @params
#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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment