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

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

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

        $QueryPage = 'table.xml'
        $ReturnData = @()
    }

    PROCESS {
        $QueryTable = @{
            "content" = "sensortree"
            "id"      = $ObjectId
            "columns" = 'objid,probe,group,device,host,downsens,partialdownsens,downacksens,upsens,warnsens,pausedsens,unusualsens,undefinedsens'
        }
        Write-Verbose "Looking up 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)
                }
            }
        }

        $Nodes = $Response.prtg.sensortree.nodes
        $global:Nodes = $Nodes

        if ($Nodes.device) {
            $Object = [PrtgDevice]::new()
            $Object.Name = $Nodes.device.name
            $Object.ObjectId = $Nodes.device.id[0]
            $Object.Hostname = $Nodes.device.host

            $ReturnData += $Object
        } else {
            Throw "Only works on devices right now"
        }
    }

    END {
        $ReturnData
    }
}