-
Notifications
You must be signed in to change notification settings - Fork 0
/
Get-ChuckNorrisJoke.ps1
53 lines (43 loc) · 1.27 KB
/
Get-ChuckNorrisJoke.ps1
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<#
.SYNOPSIS
Get a random Chuck Norris joke from an API
.EXAMPLE
PS> .\Get-ChuckNorrisJoke.ps1
.EXAMPLE
PS> .\Get-ChuckNorrisJoke.ps1 -JokeType dev
#>
[CmdletBinding()]
Param (
[ValidateSet(
'animal', 'career', 'celebrity', 'dev', 'explicit', 'fashion', 'food', 'history', 'money', 'movie', 'music', 'political', 'religion', 'science', 'sport', 'travel')]
$JokeType
)
function Get-RandomJokeType {
<#
.Description
#If JokeType parameter is not used, then pick a random one...
#>
Write-Verbose -Message "Joke type not selected, random one being chosen..."
$JokeTypes = Invoke-WebRequest -Uri "https://api.chucknorris.io/jokes/categories" | ConvertFrom-Json
[System.Collections.ArrayList]$JokeTypes = $JokeTypes
#Remove explicit to keep it PG...
$JokeTypes.Remove("explicit")
$JokeType = $JokeTypes | Get-Random
Write-Verbose "Joke Type: $JokeType"
Write-Output $JokeType
}
function Get-RandomJoke {
<#
.Description
#Get the random joke...
#>
param (
$JokeType
)
$Data = Invoke-WebRequest -Uri "https://api.chucknorris.io/jokes/random?category=$JokeType" | ConvertFrom-Json
Write-Output $Data.value
}
if (!$JokeType) {
$JokeType = Get-RandomJokeType
}
Get-RandomJoke -JokeType $JokeType