Public/Connect-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
function Connect-HealthCheck
{
<#
.SYNOPSIS
    The Connect-HealthCheck cmdlet store the API key for the HealthCheck module cmdlets.

    Connect-HealthCheck will only store the API key, no validation is done.
.DESCRIPTION
    The Connect-B2Cloud cmdlet is used to store the API key for the HealthCheck cmdlets.
    
    The API key can be obtained from your HealthCheck account page.
.EXAMPLE
    Connect-HealthCheck

    The above cmdlet will prompt for the API key and save the API key for use in the other HealthCheck modules.
.EXAMPLE
    PS C:\>Connect-B2Cloud -ApiKey YOUR_API_KEY

    The above cmdlet will take the given API key and save the API key for use in the other HealthCheck modules.
.INPUTS
    System.String

        This cmdlet takes the API key as a string.
.OUTPUTS
    None
.NOTES
    Connect-HealthCheck will only store the API key, no validation is done.
.LINK
    https://healthchecks.io/docs/
.ROLE
    PS.HealthChecks
.FUNCTIONALITY
    PS.HealthChecks
#>

    [CmdletBinding(SupportsShouldProcess=$false,
                   PositionalBinding)]
    [Alias()]
    [OutputType()]
    Param
    (
        # The API key to access the HealthCheck account.
        [Parameter(Mandatory)]
        [ValidateNotNull()]
        [ValidateNotNullOrEmpty()]
        [Alias('Key')]
        [String]$ApiKey
    )
    
    Begin
    {
        [Hashtable]$sessionHeaders = @{'X-Api-Key'=$ApiKey}
        [Uri]$hchkApiUri = 'https://healthchecks.io/api/v1/checks/'
    }
    Process
    {
        try
        {
            Invoke-RestMethod -Method Get -Uri $hchkApiUri -Headers $sessionHeaders | Out-Null
            [String]$script:SavedHealthCheckApi = $ApiKey
        }
        catch
        {
            $errorDetail = $_.Exception.Message
            Write-Error -Exception "Unable to authenticate with given APIKey.`n`r$errorDetail" `
                -Message "Unable to authenticate with given APIKey.`n`r$errorDetail" -Category AuthenticationError
        }
    }
}