Public/Get-PrtgTableData.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
function Get-PrtgTableData {
    [CmdletBinding()]

    Param (
        [Parameter(Mandatory = $True, Position = 0)]
        [ValidateSet('probes', "devices", "groups", "sensors", "todos", "messages", "values", "channels", "history")]
        [string]$Content,

        [Parameter(Mandatory = $false, Position = 1)]
        [int]$ObjectId = 0,

        [Parameter(Mandatory = $False)]
        [string[]]$Column,

        <# [Parameter(Mandatory = $False)]
        [string[]]$FilterTag, #>


        [Parameter(Mandatory = $False)]
        [int]$Count = 500,

        [Parameter(Mandatory = $False)]
        [int]$StartNumber
    )

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

        $QueryTable = @{}
        $Batching = $false

        $global:PrtgServerObject.ReturnCount = $Count
        $global:PrtgServerObject.CurrentStartPosition = $StartNumber

        <# if ($Content -eq "sensors" -and $FilterTags) {
            $FilterString = ""

            foreach ($tag in $FilterTags) {
                $FilterString += "&filter_tags=" + $tag
            }
        } #>


        $ValidColumns = @{}
        $ValidColumns.probes = @("objid", "type", "name", "tags", "active", "probe", "notifiesx", "intervalx", "access", "dependency", "probegroupdevice", "status", "message", "priority", "upsens", "downsens", "downacksens", "partialdownsens", "warnsens", "pausedsens", "unusualsens", "undefinedsens", "totalsens", "favorite", "schedule", "comments", "condition", "basetype", "baselink", "parentid", "fold", "groupnum", "devicenum")
        $ValidColumns.devices = @("objid", "probe", "group", "device", "host", "downsens", "partialdownsens", "downacksens", "upsens", "warnsens", "pausedsens", "unusualsens", "undefinedsens")
        $ValidColumns.groups = @("objid", "probe", "group", "name", "downsens", "partialdownsens", "downacksens", "upsens", "warnsens", "pausedsens", "unusualsens", "undefinedsens")
        $ValidColumns.sensors = @("parentid", "objid", "probe", "group", "device", "sensor", "status", "message", "lastvalue", "lastvalue_raw", "priority", "favorite")
        $ValidColumns.todos = @("objid", "datetime", "name", "status", "priority", "message")
        $ValidColumns.messages = @("objid", "datetime", "parent", "type", "name", "status", "message")
        $ValidColumns.values = @("datetime", "value_", "coverage")
        $ValidColumns.channels = @("name", "lastvalue", "lastvalue_raw")
        $ValidColumns.history = @("dateonly", "timeonly", "user", "message")

        $ValidColumnsForContent = $ValidColumns.$Content

        if ($Column) {
            foreach ($col in $Column) {
                if ($ValidColumnsForContent -notcontains $col) {
                    try {
                        throw
                    } catch {
                        $PSCmdlet.ThrowTerminatingError([HelperProcessError]::throwCustomError(1002, $col))
                    }
                }
            }
            $SelectedColumns = $Column
        } else {
            $SelectedColumns = $ValidColumnsForContent
        }

        $SelectedColumnsString = $SelectedColumns -join ","

        $HTMLColumns = @("downsens", "partialdownsens", "downacksens", "upsens", "warnsens", "pausedsens", "unusualsens", "undefinedsens", "message", "favorite")
        $QueryPage = 'table.xml'
        $ReturnData = @()
    }

    PROCESS {
        $QueryTable.content = $Content
        $QueryTable.columns = $SelectedColumnsString
        $QueryTable.id = $ObjectId

        if ($Batching) {

        } else {
            try {
                $Response = $global:PrtgServerObject.invokeApiQuery($QueryTable, $QueryPage, $Content)
            } 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 {
                        Throw $_
                        #$PSCmdlet.ThrowTerminatingError($PSItem)
                    }
                }
            }

            foreach ($obj in $Response) {
                switch ($Content) {
                    'devices' {
                        $ReturnData += [PrtgDevice]::new($obj)
                        continue
                    }
                    'groups' {
                        $ReturnData += [PrtgGroup]::new($obj)
                        continue
                    }
                    'probes' {
                        if ($obj.type -ne 'Probe') {
                            continue
                        }
                        $ReturnData += [PrtgProbe]::new($obj)
                        continue
                    }
                    default {
                        $ReturnData = $Response
                    }
                }
            }
        }

        $ReturnData


        <# $url = HelperURLBuilder "table.xml" (
            "&content=$Content",
            "&columns=$SelectedColumnsString",
            "&id=$ObjectId",
            $FilterString,
            $CountString
        ) #>


        #$Global:LastUrl = $Url

        <# if ($Raw) {
            $QueryObject = HelperHTTPQuery $url
            return $QueryObject.Data
        } #>

        <#
        $QueryObject = HelperHTTPQuery $url -AsXML
        $Data = $QueryObject.Data

        $ReturnData = @()

        foreach ($item in $Data.$Content.item) {
            $ThisRow = "" | Select-Object $SelectedColumns
            foreach ($Prop in $SelectedColumns) {
                if ($Content -eq "channels" -and $Prop -eq "lastvalue_raw") {
                    # fix a bizarre formatting bug
                    $ThisRow.$Prop = HelperFormatHandler $item.$Prop
                } elseif ($HTMLColumns -contains $Prop) {
                    # strip HTML, leave bare text
                    $ThisRow.$Prop = $item.$Prop -replace "<[^>]*?>|<[^>]*>", ""
                } else {
                    $ThisRow.$Prop = $item.$Prop
                }
            }
            $ReturnData += $ThisRow
        }

        if ($ReturnData.name -eq "Item" -or (!($ReturnData.ToString()))) {
            $DeterminedObjectType = Get-PrtgObjectType $ObjectId

            $ValidQueriesTable = @{
                group = @("devices", "groups", "sensors", "todos", "messages", "values", "history")
                probenode = @("devices", "groups", "sensors", "todos", "messages", "values", "history")
                device = @("sensors", "todos", "messages", "values", "history")
                sensor = @("messages", "values", "channels", "history")
                report = @("Currently unsupported")
                map = @("Currently unsupported")
                storedreport = @("Currently unsupported")
            }

            Write-Host "No $Content; Object $ObjectId is type $DeterminedObjectType"
            Write-Host (" Valid query types: " + ($ValidQueriesTable.$DeterminedObjectType -join ", "))
        } else {
            return $ReturnData
        } #>

    }
}