-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFreeDadJokes.psm1
33 lines (26 loc) · 922 Bytes
/
FreeDadJokes.psm1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
function Get-DadJoke() {
<#
.SYNOPSIS
Get a Random Dad Joke on the command line.
.DESCRIPTION
This function retrieves a random dad joke from the internet.
.EXAMPLE
Get-DadJoke
This example retrieves a random dad joke.
.NOTES
Author: Michael Free
.OUTPUTS
System.String. This function outputs two lines of text to the console.
#>
$jokeAPI = 'https://official-joke-api.appspot.com/random_joke'
$testJokeAPI = Invoke-WebRequest -Uri $jokeAPI -Method HEAD -UseBasicParsing
if (-not $testJokeAPI.StatusCode -eq 200) {
throw 'Joke API Route Unavailable'
}
$dadJoke = Invoke-RestMethod -Uri $jokeAPI -Method Get
if (-not ($dadJoke | Get-Member -Name 'setup') -and -not ($dadJoke | Get-Member -Name 'punchline')) {
throw 'setup and punchline object unavailabe in response'
}
Write-Output $dadJoke.setup
Write-Output $dadJoke.punchline
}