Public/Get-PrtgObjectProperty.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
function Get-PrtgObjectProperty {
    [CmdletBinding()]

    Param (
        [Parameter(Mandatory = $True, Position = 0, ValueFromPipeline = $True, ValueFromPipelineByPropertyName = $True)]
        [int[]]$ObjectId,

        [Parameter(Mandatory = $True, Position = 1)]
        [string]$Property
    )

    BEGIN {
        $VerbosePrefix = "Get-PrtgObjectProperty:"
        if (!($global:PrtgServerObject.Connected)) {
            try {
                Throw
            } catch {
                $PSCmdlet.ThrowTerminatingError([HelperProcessError]::throwCustomError(1000, $global:PrtgServerObject.Hostname))
            }
        }

        $QueryTable = @{}
        $QueryTable.name = $Property

        $QueryPage = 'getobjectproperty.htm'
        $ReturnData = @()
    }

    PROCESS {
        $QueryTable.id = $ObjectId
        Write-Verbose "Looking up $Property for Object $ObjectId"

        try {
            $Response = $global:PrtgServerObject.invokeApiQuery($QueryTable, $QueryPage)
        } catch {
            # originally I was catching specific types of exceptions, but apparently they're different between core and non-core, which is stupid
            switch -Regex ($_.Exception.Message) {
                '401\ \(Unauthorized\)' {
                    $PSCmdlet.ThrowTerminatingError([HelperProcessError]::throwCustomError(1001, $Server))
                }
                default {
                    $PSCmdlet.ThrowTerminatingError($PSItem)
                }
            }
        }

        $ReturnData += $Response.prtg.result
    }

    END {
        $ReturnData
    }
}