One of the things I love to do is add a Dad joke to my reports. Reddit has some good ones. What’s cool about Reddit is they have a JSON backend that can be used and Used I do.

The Function

Function Get-DadJoke {
    $DadJoke = Invoke-RestMethod -Uri "https://www.reddit.com/r/dadjokes/top.json" -UseBasicParsing
    $Joke = $DadJoke.data.children[(Get-Random -Minimum 0 -Maximum $($dadjoke.data.children.count - 1))]
    Write-Host "$($Joke.data.title)"
    Write-Host "$($Joke.data.selftext)"
}

The Breakdown

This script is super simple. We are using a rest method to grab the JSON information. Wrapping it in a function with a write-host. Nothing more simple.

$DadJoke = Invoke-RestMethod -Uri "https://www.reddit.com/r/dadjokes/top.json" -UseBasicParsing

The first part is the dad jokes themselves. We are grabbing the top jokes on the subreddit. We use the Invoke-RestMethod because we are grabbing that JSON.

$Joke = $DadJoke.data.children[(Get-Random -Minimum 0 -Maximum $($dadjoke.data.children.count - 1))]

The next line grabs a random Joke from the list. The $DadJoke.Data.Children are an array. We are grabbing a random index from the array where the minimum is 0. The maximum is the number of arrays minus one. We do a minus one because everything starts at 0.

Write-Host "$($Joke.data.title)"
    Write-Host "$($Joke.data.selftext)"

Finally, we write-host out the information. Notice once again, we use the $() structure. This way we can grab the subarrays of each item and displays the information accordingly.

A very simple breakdown of the function, and I hope you all enjoyed it.