Public/Get-HealthCheck.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
using module ..\Class\Check.Class.psm1
function Get-HealthCheck
{
<#
.SYNOPSIS
    The Get-HealthCheck cmdlet will list all checks associated with the account.
.DESCRIPTION
    The Get-HealthCheck cmdlet will list all checks associated with the account.

    An API key is required to use this cmdlet.
.EXAMPLE
    Get-HealthCheck

    Grace : 900
    LastPing : Saturday, July 9, 2016 6:58:43 AM
    PingCount : 1
    Name : Api test 1
    NextPing : Saturday, July 9, 2016 7:58:43 AM
    PingURL : https://hchk.io/25c55e7c-8092-4d21-ad06-7dacfbb6fc10
    Tags : foo
    Timeout : 3600

    Grace : 60
    LastPing :
    PingCount : 0
    Name : Api test 2
    NextPing :
    PingURL : https://hchk.io/7e1b6e61-b16f-4671-bae3-e3233edd1b5e
    Tags : bar baz
    Timeout : 60

    The cmdlet above will return all checks for the account.
.EXAMPLE
    PS C:\>Get-B2Bucket | Where-Object {$_.Name -eq 'Api test 2'}

    Grace : 60
    LastPing :
    PingCount : 0
    Name : Api test 2
    NextPing :
    PingURL : https://hchk.io/7e1b6e61-b16f-4671-bae3-e3233edd1b5e
    Tags : bar baz
    Timeout : 60

    The cmdlet above will return all buckets and search for the one with a name of "Api test 2".
.INPUTS
    System.String

        This cmdlet takes the AccountID and ApplicationKey as strings.
.OUTPUTS
    Check

        This cmdlet returns a check object for each check found.
.LINK
    https://healthchecks.io/docs/
.ROLE
    PS.HealthChecks
.FUNCTIONALITY
    PS.HealthChecks
#>

    [CmdletBinding(SupportsShouldProcess=$false,
                   PositionalBinding)]
    [Alias()]
    [OutputType([Check])]
    Param
    (
        # The Api key for your account.
        [Parameter(Mandatory=$false)]
        [ValidateNotNull()]
        [ValidateNotNullOrEmpty()]
        [Uri]$ApiKey = $script:SavedHealthCheckApi
    )

    Begin
    {
        if($ApiKey -eq $null){ throw 'The API key needs to be specified.' }
        # By default PowerShell will not accept TLS 1.2 connections.
        # This can be fixed by running the code below.
        try {
            [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
        }
        catch {
            throw 'Unable to set PowerShell to accept TLS 1.2 connections, unable to continue.'
        }
        [Hashtable]$sessionHeaders = @{'X-Api-Key'=$ApiKey}
        [Uri]$hchkApiUri = 'https://healthchecks.io/api/v1/checks/'
    }
    Process
    {
        $hchkInfo = Invoke-RestMethod -Method Get -Uri $hchkApiUri -Headers $sessionHeaders
        foreach($info in $hchkInfo.checks)
        {
            $hchkReturnInfo = [Check]::New($info.name,
                                           $info.tags,
                                           $info.timeout,
                                           $info.grace,
                                           $info.ping_url,
                                           $info.n_pings,
                                           $info.last_ping,
                                           $info.next_ping)
            Write-Output $hchkReturnInfo
        }
    }
}