Functions/Get-Monitor.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
function Get-Monitor {
    <#
    .SYNOPSIS
    Retrieves a list of monitors from Uptime Robot
     
    .DESCRIPTION
    Implements the GetMonitors endpoint of the Uptime Robot V2 API.
 
    .PARAMETER ApiKey
    The account or monitor ApiKey for your Uptime Robot account.
 
    .PARAMETER ID
    The ids of one or more monitors to retrieve.
 
    .PARAMETER Type
    One or more types of monitors to retrieve.
 
    .PARAMETER Status
    Filter monitor list by one or more status values.
 
    .PARAMETER Logs
    Retrieves the logs associated with each monitor.
 
    .PARAMETER ResponseTimes
    Retrieves the response times associated with each monitor.
 
    .PARAMETER AlertContacts
    Retrieves the alert contacts asssociated with each monitor.
 
    .EXAMPLE
    Get-URMonitor -ApiKey '12345-12345'
 
    Gets all configured monitors
     
    .EXAMPLE
    Get-URMonitor -ID 98765,87654 -ApiKey '12345-12345'
 
    Gets the monitors with ids '98765' and '87654'
 
    .EXAMPLE
    Get-URMonitor -Type HTTP -ApiKey '12345-12345'
 
    Gets all monitors with the HTTP type.
 
    .NOTES
    General notes
    #>

    Param(
        [Parameter(Mandatory=$true)]
        [string]$ApiKey,

        [int[]]$ID,

        [ValidateSet("HTTP","Keyword","Ping","Port")]
        [string[]]$Type,

        [ValidateSet("Paused","NotCheckedYet","Up","SeemsDown","Down")]
        [string[]]$Status,

        [switch]$Logs,

        [switch]$ResponseTimes,

        [switch]$AlertContacts
    )

    Begin {
        [uri]$uri = "$urBaseUri/getMonitors"

        $body = "api_key=$ApiKey&format=json"

        if ( $ID ) { $body += "&monitors={0}" -f ( $ID -join "-" ) }
        if ( $Type ) { $body += "&types={0}" -f ( $Type.ForEach({ ([UptimeRobotMonitorType]::"$_").value__ }) -join "-" ) }
        if ( $Status ) { $body += "&statuses={0}" -f ( $Status.ForEach({ ([UptimeRobotMonitorStatus]::"$_").value__ }) -join "-" ) }
        if ( $Logs ) { $body += "&logs={0}" -f [int]$Logs.ToBool() }
        if ( $ResponseTimes ) { $body += "&response_Times={0}" -f [int]$ResponseTimes.ToBool() }
        if ( $AlertContacts ) { $body += "&alert_contacts={0}" -f [int]$AlertContacts.ToBool() }
    }

    Process {
        $result = Invoke-RestMethod -Method Post -UseBasicParsing -Uri $uri.AbsoluteUri -Body $body -ContentType "application/x-www-form-urlencoded"
    }

    End {
        Write-Output $result
    }
}