Public/Find-XKCD.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
Function Find-XKCD {
    <#
    .SYNOPSIS
        Retrieves the details of comics @ http://xkcd.com/ based on whether a specified search string appears
        in the title text.
 
    .DESCRIPTION
        The Find-XKCD cmdlet creates a local cache of the XKCD API comic data if one is not found to already
        exist. It also refreshes the local cache if it's found to be out of date. Comic searches are then
        performed against the local cache.
 
    .EXAMPLE
        Find-XKCD -Query 'Spider' | Format-Table
 
        Returns any comics with the word 'Spider' in the title as a table.
     
    .EXAMPLE
        Find-XKCD -Query 'Spider' | Get-XKCD -Open
 
        Returns any comics with the word 'Spider' in the title and then pipes the result to Get-XKCD which opens
        them in the default browser.
 
    .LINK
        https://xkcd.com/json.html
    #>

    [cmdletbinding()]
    Param(
        # The search string to find
        [Parameter(Mandatory)]
        [string]
        $Query,

        # Path to where comic data is cached
        [string]
        $CachePath = (Join-Path $PSScriptRoot 'XKCD.json')
    )
    
    Update-XKCDCache -CachePath $CachePath

    $AllComics = Get-Content $CachePath | ConvertFrom-Json

    $AllComics | Where-Object { $_.Title -like "*$Query*" }
}