-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprtgshell2.psm1
More file actions
1581 lines (1163 loc) · 91.9 KB
/
Copy pathprtgshell2.psm1
File metadata and controls
1581 lines (1163 loc) · 91.9 KB
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
###############################################################################
## Start Powershell Cmdlets
###############################################################################
###############################################################################
# Get-PrtgObject
function Get-PrtgObject {
Param (
[Parameter(Mandatory=$false,Position=0)]
[int]$ObjectId = 0
)
BEGIN {
if ($PRTG.Protocol -eq "https") { $PRTG.OverrideValidation() }
}
PROCESS {
$Parameters = @{
"content" = "sensortree"
"id" = $ObjectId
}
$url = $PrtgServerObject.UrlBuilder("api/table.xml",$Parameters)
##### data returned; do!
if ($Raw) {
$QueryObject = HelperHTTPQuery $url
return $QueryObject.Data
}
$QueryObject = HelperHTTPQuery $url -AsXML
$Data = $QueryObject.Data
$DeviceType = $Data.prtg.sensortree.nodes.SelectNodes("*[1]").LocalName
$ObjectXMLData = $Data.prtg.sensortree.nodes.SelectNodes("*[1]")
####
$TestReturn = "" | select type,data
$TestReturn.type = $DeviceType
$TestReturn.data = $ObjectXMLData
return $TestReturn
####
#$ReturnData = @()
<#
HOW THIS WILL LIKELY NEED TO WORK
---
build a switch statement that uses $Content to determine which types of objects we're going to create
foreach item, assign all properties to the object
attach the object to $ReturnData
#>
$PrtgObjectType = switch ($DeviceType) {
"probes" { "PrtgShell.PrtgProbe" }
"groups" { "PrtgShell.PrtgGroup" }
"devices" { "PrtgShell.PrtgDevice" }
"sensors" { "PrtgShell.PrtgSensor" }
"todos" { "PrtgShell.PrtgTodo" }
"messages" { "PrtgShell.PrtgMessage" }
"values" { "PrtgShell.PrtgValue" }
"channels" { "PrtgShell.PrtgChannel" }
"history" { "PrtgShell.PrtgHistory" }
}
$ObjectXMLData = $Data.prtg.sensortree.nodes.SelectNodes("*[1]")
$ThisObject = New-Object $PrtgObjectType
foreach ($p in $ObjectXMLData.GetEnumerator()) {
$ThisObject.($p.name) = $p.'#Text'
}
return $ThisObject
<#
#$ThisRow = "" | Select-Object $SelectedColumns
foreach ($Prop in $SelectedColumns) {
if ($Content -eq "channels" -and $Prop -eq "lastvalue_raw") {
# fix a bizarre formatting bug
#$ThisObject.$Prop = HelperFormatHandler $item.$Prop
$ThisObject.$Prop = $item.$Prop
} elseif ($HTMLColumns -contains $Prop) {
# strip HTML, leave bare text
$ThisObject.$Prop = $item.$Prop -replace "<[^>]*?>|<[^>]*>", ""
} else {
$ThisObject.$Prop = $item.$Prop
}
}
$ReturnData += $ThisObject
}
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
}
#>
}
}
###############################################################################
# Get-PrtgObjectProperty
function Get-PrtgObjectProperty {
<#
.SYNOPSIS
.DESCRIPTION
.EXAMPLE
#>
Param (
[Parameter(Mandatory=$True,Position=0)]
[alias('DeviceId')]
[int]$ObjectId,
[Parameter(Mandatory=$True,Position=1)]
[string]$Property
)
BEGIN {
if (!($PrtgServerObject.Server)) { Throw "Not connected to a server!" }
}
PROCESS {
$Url = $PrtgServerObject.UrlBuilder("api/getobjectproperty.htm",@{
"id" = $ObjectId
"name" = $Property
"show" = "text"
})
$Data = $PrtgServerObject.HttpQuery($Url,$true)
return $Data.Data.prtg.result
}
}
###############################################################################
# Get-PrtgSensorHistoricData
function Get-PrtgSensorHistoricData {
<#
.SYNOPSIS
Returns historic data from a specified time period from a sensor object.
.DESCRIPTION
Returns a table of data using the specified start and end dates and the specified interval.
.PARAMETER SensorId
The sensor to retrieve data for.
.PARAMETER RangeStart
DateTime object specifying the start of the history range.
.PARAMETER RangeEnd
DateTime object specifying the End of the history range.
.PARAMETER IntervalInSeconds
The minimum interval to include, in seconds. The default is one hour (3600 seconds). A value of zero (0) will return raw data.
.EXAMPLE
Get-PrtgSensorHistoricData 2321 (Get-Date "2016-06-23 12:15") (Get-Date "2016-06-23 16:15") 60
#>
[CmdletBinding()]
Param (
[Parameter(Mandatory=$True,Position=0)]
[int] $SensorId,
[Parameter(Mandatory=$True,Position=1)]
[datetime] $RangeStart,
[Parameter(Mandatory=$True,Position=2)]
[datetime] $RangeEnd,
[Parameter(Mandatory=$True,Position=3)]
[int] $IntervalInSeconds = 3600
)
BEGIN {
$PrtgServerObject = $Global:PrtgServerObject
}
PROCESS {
$Parameters = @{
"id" = $SensorId
"sdate" = $RangeStart.ToString("yyyy-MM-dd-HH-mm-ss")
"edate" = $RangeEnd.ToString("yyyy-MM-dd-HH-mm-ss")
"avg" = $IntervalInSeconds
}
$url = $PrtgServerObject.UrlBuilder("api/historicdata.csv",$Parameters)
$QueryObject = $PrtgServerObject.HttpQuery($url,$false)
$DataPoints = $QueryObject.RawData | ConvertFrom-Csv | ? { $_.'Date Time' -ne 'Averages' }
}
END {
return $DataPoints
}
}
###############################################################################
# Get-PrtgSensorUptime
# optional thing to add here:
# make it so we can define a target month in this report, rather than manually specifying the start and end date.
function Get-PrtgSensorUptime {
<#
.SYNOPSIS
Returns five-nines-style uptime for a specified time period from a sensor object.
.DESCRIPTION
Returns five-nines-style uptime for a specified time period from a sensor object.
.PARAMETER SensorId
The sensor to retrieve data for.
.PARAMETER RangeStart
DateTime object specifying the start of the history range.
.PARAMETER RangeEnd
DateTime object specifying the End of the history range.
.EXAMPLE
Get-PrtgSensorUptime 2321 (Get-Date "2016-06-23 12:15") (Get-Date "2016-06-23 16:15")
#>
[CmdletBinding()]
Param (
[Parameter(Mandatory=$true,Position=0)]
[int] $SensorId,
[Parameter(Mandatory=$true,Position=1)]
[datetime] $RangeStart,
[Parameter(Mandatory=$false,Position=2)]
[datetime] $RangeEnd
)
BEGIN {
$PrtgServerObject = $Global:PrtgServerObject
if (!$RangeEnd) {
$RangeStart = Get-Date ($RangeStart.ToString('MMMM yyyy'))
$RangeEnd = $RangeStart.AddMonths(1).AddSeconds(-1)
}
}
PROCESS {
$ObjectInterval = Get-PrtgObject $SensorId | Select-Object -ExpandProperty interval
$HistoricData = Get-PrtgSensorHistoricData $SensorId $RangeStart $RangeEnd 0
$APropertyName = (($HistoricData | Get-Member -MemberType NoteProperty | Select-Object -ExpandProperty Name) -notmatch "Coverage") -notmatch "Date Time" | Select-Object -First 1
# maybe this is valid?
$UpEntries = $HistoricData.$APropertyName | ? { $_ -ne "" }
}
END {
$returnobject = "" | select SensorId,RangeStart,RangeEnd,TotalDatapoints,UpDatapoints,DownDatapoints,Interval,UptimePercentage
$returnobject.SensorId = $SensorId
$returnobject.RangeStart = $RangeStart
$returnobject.RangeEnd = $RangeEnd
$returnobject.TotalDatapoints = $HistoricData.Count
$returnobject.UpDatapoints = $UpEntries.Count
$returnobject.DownDatapoints = $HistoricData.Count - $UpEntries.Count
$returnobject.Interval = $ObjectInterval
if ($HistoricData.Count) {
$returnobject.UptimePercentage = ($UpEntries.Count / $HistoricData.Count) * 100
} else {
$returnobject.UptimePercentage = 0
}
return $returnobject
}
}
###############################################################################
# Get-PrtgServer
function Get-PrtgServer {
<#
.SYNOPSIS
Establishes initial connection to PRTG API.
.DESCRIPTION
The Get-PrtgServer cmdlet establishes and validates connection parameters to allow further communications to the PRTG API. The cmdlet needs at least three parameters:
- The server name (without the protocol)
- An authenticated username
- A passhash that can be retrieved from the PRTG user's "My Account" page.
The cmdlet returns an object containing details of the connection, but this can be discarded or saved as desired; the returned object is not necessary to provide to further calls to the API.
.EXAMPLE
Get-PrtgServer "prtg.company.com" "jsmith" 1234567890
Connects to PRTG using the default port (443) over SSL (HTTPS) using the username "jsmith" and the passhash 1234567890.
.EXAMPLE
Get-PrtgServer "prtg.company.com" "jsmith" 1234567890 -HttpOnly
Connects to PRTG using the default port (80) over SSL (HTTP) using the username "jsmith" and the passhash 1234567890.
.EXAMPLE
Get-PrtgServer -Server "monitoring.domain.local" -UserName "prtgadmin" -PassHash 1234567890 -Port 8080 -HttpOnly
Connects to PRTG using port 8080 over HTTP using the username "prtgadmin" and the passhash 1234567890.
.PARAMETER Server
Fully-qualified domain name for the PRTG server. Don't include the protocol part ("https://" or "http://").
.PARAMETER UserName
PRTG username to use for authentication to the API.
.PARAMETER PassHash
PassHash for the PRTG username. This can be retrieved from the PRTG user's "My Account" page.
.PARAMETER Port
The port that PRTG is running on. This defaults to port 443 over HTTPS, and port 80 over HTTP.
.PARAMETER HttpOnly
When specified, configures the API connection to run over HTTP rather than the default HTTPS.
.PARAMETER Quiet
When specified, the cmdlet returns nothing on success.
#>
[CmdletBinding()]
Param (
[Parameter(Mandatory=$True,Position=0)]
[ValidatePattern("\d+\.\d+\.\d+\.\d+|(\w\.)+\w")]
[string]$Server,
[Parameter(Mandatory=$True,Position=1)]
[string]$UserName,
[Parameter(Mandatory=$True,Position=2)]
[string]$PassHash,
[Parameter(Mandatory=$False,Position=3)]
[int]$Port = $null,
[Parameter(Mandatory=$False)]
[alias('http')]
[switch]$HttpOnly,
[Parameter(Mandatory=$False)]
[alias('q')]
[switch]$Quiet
)
BEGIN {
$PrtgServerObject = New-Object PrtgShell.PrtgServer
$PrtgServerObject.Server = $Server
$PrtgServerObject.UserName = $UserName
$PrtgServerObject.PassHash = $PassHash
if ($HttpOnly) {
$Protocol = "http"
if (!$Port) { $Port = 80 }
} else {
$Protocol = "https"
if (!$Port) { $Port = 443 }
#$PrtgServerObject.OverrideValidation()
}
$PrtgServerObject.Protocol = $Protocol
$PrtgServerObject.Port = $Port
}
PROCESS {
$url = $PrtgServerObject.UrlBuilder("api/getstatus.xml")
try {
#$QueryObject = HelperHTTPQuery $url -AsXML
#$PrtgServerObject.OverrideValidation()
$QueryObject = $PrtgServerObject.HttpQuery($url)
} catch {
throw "Error performing HTTP query"
}
$Data = $QueryObject.Data
# the logic and future-proofing of this is a bit on the suspect side.
# the idea is that we want to get all the properties that it returns
# and shove them into our new object, but if the object is missing
# the property in the first place we will get an error. this happens
# periodically when paessler adds new properties to the output.
#
# so how do we gracefully handle new properties?
foreach ($ChildNode in $data.status.ChildNodes) {
# for now, we outright ignore them.
if (($PrtgServerObject | Get-Member | Select-Object -ExpandProperty Name) -contains $ChildNode.Name) {
if ($ChildNode.Name -ne "IsAdminUser") {
$PrtgServerObject.$($ChildNode.Name) = $ChildNode.InnerText
} else {
# TODO
# there's at least four properties that need to be treated this way
# this is because this property returns a text "true" or "false", which powershell always evaluates as "true"
$PrtgServerObject.$($ChildNode.Name) = [System.Convert]::ToBoolean($ChildNode.InnerText)
}
}
}
$global:PrtgServerObject = $PrtgServerObject
#HelperFormatTest ###### need to add this back in
# this tests for a decimal-placement bug that existed in the output from some old versions of prtg
if (!$Quiet) {
return $PrtgServerObject | Select-Object @{n='Connection';e={$_.ApiUrl}},UserName,Version
}
}
}
###############################################################################
# Get-PrtgStatus
function Get-PrtgStatus {
# this is nowhere near complete or useful. the data returned by this control is tagged HTML with untagged, unlabelled, unidentified data, which could be immensely useful. if it was structured.
BEGIN {
if ($PRTG.Protocol -eq "https") { $PRTG.OverrideValidation() }
}
PROCESS {
$Parameters = @{
"content" = "sensortree"
"id" = $ObjectId
}
$url = $PrtgServerObject.UrlBuilder("controls/systemstatus.htm")
$QueryObject = HelperHTTPQuery $url
return $QueryObject.Data
}
}
###############################################################################
# Get-PrtgTableData
function Get-PrtgTableData {
<#
.SYNOPSIS
Returns a PowerShell object containing data from the specified object in PRTG.
.DESCRIPTION
The Get-PrtgTableData cmdlet can return data of various different content types using the specified parent object, as well as specify the return columns or filtering options. The input formats generally coincide with the Live Data demo from the PRTG API documentation, but there are some content types that the cmdlet does not yet support, such as "sensortree".
.PARAMETER Content
The type of data to return about the specified object. Valid values are "devices", "groups", "sensors", "todos", "messages", "values", "channels", and "history". Note that all content types are not valid for all object types; for example, a device object can contain no groups or channels.
.PARAMETER ObjectId
An object ID from PRTG. Objects include probes, groups, devices, and sensors, as well as reports, maps, and todos.
.PARAMETER Columns
A string array of named column values to return. In general the default return values for a given content type will return all of the available columns; this parameter can be used to change the order of columns or specify which columns to include or ignore.
.PARAMETER FilterTags
A string array of sensor tags. This parameter only has any effect if the content type is "sensor". Output will only include sensors with the specified tags. Note that specifying multiple tags performs a logical OR of tags.
.PARAMETER Count
Number of records to return. PRTG's internal default for this is 500. Valid values are 1-50000.
.PARAMETER Raw
If this switch is set, the cmdlet will return the raw XML data rather than a PowerShell object.
.EXAMPLE
Get-PrtgTableData groups 1
Returns the groups under the object ID 1, which is typically the Core Server's Local Probe.
.EXAMPLE
Get-PrtgTableData sensors -FilterTags corestatesensor,probesensor
Returns a filtered list of sensors tagged with "corestatesensor" or "probesensor".
.EXAMPLE
Get-PrtgTableData messages 1002
Returns the messages log for device 1002.
#>
[CmdletBinding()]
Param (
[Parameter(Mandatory=$True,Position=0)]
[ValidateSet("probes","groups","devices","sensors","todos","messages","values","channels","history","maps")]
[string]$Content,
[Parameter(Mandatory=$false,Position=1)]
[int]$ObjectId = 0,
[Parameter(Mandatory=$False)]
[string[]]$Columns,
[Parameter(Mandatory=$False)]
[string[]]$FilterTags,
[Parameter(Mandatory=$False)]
[ValidateSet("Unknown","Collecting","Up","Warning","Down","NoProbe","PausedbyUser","PausedbyDependency","PausedbySchedule","Unusual","PausedbyLicense","PausedUntil","DownAcknowledged","DownPartial")]
[string[]]$FilterStatus,
[Parameter(Mandatory=$False)]
[string]$FilterTarget,
[Parameter(Mandatory=$False)]
[string]$FilterValue,
[Parameter(Mandatory=$False)]
[int]$Count,
[Parameter(Mandatory=$False)]
[switch]$Raw
)
<# things to add
filter_drel (content = messages only) today, yesterday, 7days, 30days, 12months, 6months - filters messages by timespan
filter_status (content = sensors only) Unknown=1, Collecting=2, Up=3, Warning=4, Down=5, NoProbe=6, PausedbyUser=7, PausedbyDependency=8, PausedbySchedule=9, Unusual=10, PausedbyLicense=11, PausedUntil=12, DownAcknowledged=13, DownPartial=14 - filters messages by status
sortby = sorts on named column, ascending (or decending with a leading "-")
filter_xyz - fulltext filtering. this is a feature in its own right
#>
BEGIN {
$PRTG = $Global:PrtgServerObject
$CountProperty = @{}
$FilterProperty = @{}
if ($Count) {
$CountProperty = @{ "count" = $Count }
}
if ($FilterTags -and (!($Content -eq "sensors"))) {
throw "Get-PrtgTableData: Parameter FilterTags requires content type sensors"
} elseif ($Content -eq "sensors" -and $FilterTags) {
$FilterProperty += @{ "filter_tags" = $FilterTags }
}
$StatusFilterCodes = @{
"Unknown" = 1
"Collecting" = 2
"Up" = 3
"Warning" = 4
"Down" = 5
"NoProbe" = 6
"PausedbyUser" = 7
"PausedbyDependency" = 8
"PausedbySchedule" = 9
"Unusual" = 10
"PausedbyLicense" = 11
"PausedUntil" = 12
"DownAcknowledged" = 13
"DownPartial" = 14
}
if ($FilterStatus -and (!($Content -eq "sensors"))) {
throw "Get-PrtgTableData: Parameter FilterStatus requires content type sensors"
} elseif ($Content -eq "sensors" -and $FilterStatus) {
# I apparently wrote some code that gracefully
# handles this (multiple properties w/ same name) two years ago.
# good job, past josh
$FilterProperty += @{ "filter_status" = $StatusFilterCodes[$FilterStatus] }
}
if ($FilterTarget) {
if (!$FilterValue) {
throw "Get-PrtgTableData: Parameter FilterTarget requires parameter FilterValue also"
}
$FilterName = "filter_" + $FilterTarget
$FilterProperty += @{ $FilterName = $FilterValue }
}
if (!$Columns) {
# this function currently doesn't work with "sensortree" or "maps"
$TableLookups = @{
"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")
"groups" = @("objid","type","name","tags","active","group","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","location","fold","groupnum","devicenum")
"devices" = @("objid","type","name","tags","active","device","group","probe","grpdev","notifiesx","intervalx","access","dependency","probegroupdevice","status","message","priority","upsens","downsens","downacksens","partialdownsens","warnsens","pausedsens","unusualsens","undefinedsens","totalsens","favorite","schedule","deviceicon","comments","host","basetype","baselink","icon","parentid","location")
"sensors" = @("objid","type","name","tags","active","downtime","downtimetime","downtimesince","uptime","uptimetime","uptimesince","knowntime","cumsince","sensor","interval","lastcheck","lastup","lastdown","device","group","probe","grpdev","notifiesx","intervalx","access","dependency","probegroupdevice","status","message","priority","lastvalue","lastvalue_raw","upsens","downsens","downacksens","partialdownsens","warnsens","pausedsens","unusualsens","undefinedsens","totalsens","favorite","schedule","minigraph","comments","basetype","baselink","parentid")
"channels" = @("objid","name","lastvalue","lastvalue_raw")
"todos" = @("objid","datetime","name","status","priority","message","active")
"messages" = @("objid","datetime","parent","type","name","status","message")
"values" = @("datetime","value_","coverage")
"history" = @("datetime","dateonly","timeonly","user","message")
"storedreports" = @("objid","name","datetime","size")
"reports" = @("objid","name","template","period","schedule","email","lastrun","nextrun")
"maps" = @("objid","name")
}
$SelectedColumns = $TableLookups.$Content
} else {
$SelectedColumns = $Columns
}
$SelectedColumnsString = $SelectedColumns -join ","
$HTMLColumns = @("downsens","partialdownsens","downacksens","upsens","warnsens","pausedsens","unusualsens","undefinedsens","message","favorite")
}
PROCESS {
$Parameters = @{
"content" = $Content
"columns" = $SelectedColumnsString
"id" = $ObjectId
} ################################################# needs to handle filters!
$Parameters += $CountProperty
$Parameters += $FilterProperty
$url = $PrtgServerObject.UrlBuilder("api/table.xml",$Parameters)
##### data returned; do!
if ($Raw) {
$QueryObject = $PrtgServerObject.HttpQuery($url,$false)
return $QueryObject.Data
}
$QueryObject = $PrtgServerObject.HttpQuery($url)
$Data = $QueryObject.Data
$ReturnData = @()
<#
HOW THIS WILL LIKELY NEED TO WORK
---
build a switch statement that uses $Content to determine which types of objects we're going to create
foreach item, assign all properties to the object
attach the object to $ReturnData
#>
$PrtgObjectType = switch ($Content) {
"probes" { "PrtgShell.PrtgProbe" }
"groups" { "PrtgShell.PrtgGroup" }
"devices" { "PrtgShell.PrtgDevice" }
"sensors" { "PrtgShell.PrtgSensor" }
"todos" { "PrtgShell.PrtgTodo" }
"messages" { "PrtgShell.PrtgMessage" }
"values" { "PrtgShell.PrtgValue" }
"channels" { "PrtgShell.PrtgChannel" }
"history" { "PrtgShell.PrtgHistory" }
"maps" { "PrtgShell.PrtgBaseObject" }
}
if ($Data.$Content.item.childnodes.count) { # this will return zero if there's an empty set
foreach ($item in $Data.$Content.item) {
$ThisObject = New-Object $PrtgObjectType
#$ThisRow = "" | Select-Object $SelectedColumns
foreach ($Prop in $SelectedColumns) {
if ($Content -eq "channels" -and $Prop -eq "lastvalue_raw") {
# fix a bizarre formatting bug
#$ThisObject.$Prop = HelperFormatHandler $item.$Prop
$ThisObject.$Prop = $item.$Prop
} elseif ($HTMLColumns -contains $Prop) {
# strip HTML, leave bare text
$ThisObject.$Prop = $item.$Prop -replace "<[^>]*?>|<[^>]*>", ""
} else {
$ThisObject.$Prop = $item.$Prop
}
}
$ReturnData += $ThisObject
}
} else {
$ErrorString = "Object" + $ObjectId + " contains no objects of type" + $Content
if ($FilterProperty.Count) {
$ErrorString += " matching specified filter parameters"
}
Write-Host $ErrorString
}
<#
# this section needs to be revisited
# if the filter ends up returning an empty set, we need to say so, or return said empty said
# and we also need to make the "get-prtgobjecttype" cmdlet that this depends on
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
}
#>
return $ReturnData
}
}
###############################################################################
# Move-PrtgObject
function Move-PrtgObject {
<#
.SYNOPSIS
.DESCRIPTION
.EXAMPLE
#>
Param (
[Parameter(Mandatory=$True,Position=0)]
[int]$ObjectId,
[Parameter(Mandatory=$True,Position=1)]
[int]$TargetGroupId
)
BEGIN {
if (!($PrtgServerObject.Server)) { Throw "Not connected to a server!" }
}
PROCESS {
$Url = $PrtgServerObject.UrlBuilder("moveobjectnow.htm",@{
"id" = $ObjectId
"targetid" = $TargetGroupId
"approve" = 1
})
$Data = $PrtgServerObject.HttpQuery($Url,$false)
return $Data | select HttpStatusCode,Statuscode
}
}
###############################################################################
# New-PrtgDevice
function New-PrtgDevice {
Param (
[Parameter(Mandatory=$True,Position=0)]
[PrtgShell.PrtgDeviceCreator]$PrtgObject
)
BEGIN {
if (!($PrtgServerObject.Server)) { Throw "Not connected to a server!" }
$PrtgServerObject.OverrideValidation()
}
PROCESS {
$Url = $PrtgServerObject.UrlBuilder("adddevice2.htm")
HelperHTTPPostCommand $Url $PrtgObject.QueryString
}
}
###############################################################################
# New-PrtgGroup
function New-PrtgGroup {
Param (
[Parameter(Mandatory=$True,Position=0)]
[PrtgShell.PrtgGroupCreator]$PrtgObject
)
BEGIN {
if (!($PrtgServerObject.Server)) { Throw "Not connected to a server!" }
$PrtgServerObject.OverrideValidation()
}
PROCESS {
$Url = $PrtgServerObject.UrlBuilder("addgroup2.htm")
HelperHTTPPostCommand $Url $PrtgObject.QueryString | Out-Null
}
}
###############################################################################
# New-PrtgResult
function New-PrtgResult {
<#
.SYNOPSIS
Creates a PrtgShell.XmlResult object for use in ExeXml output.
.DESCRIPTION
Creates a PrtgShell.XmlResult object for use in ExeXml output.
.PARAMETER Channel
Name of the channel.
.PARAMETER Value
Integer value of the channel.
.PARAMETER Unit
Unit of the value.
.PARAMETER SpeedSize
Size of the value given, used for speed measurements.
.PARAMETER VolumeSize
Size of the value given, used for disk/file measurements.
.PARAMETER SpeedTime
Interval for displaying a speed measurement.
.PARAMETER Difference
Set the value as a difference value, as opposed to absolute.
.PARAMETER DecimalMode
Set the decimal display mode.
.PARAMETER Warning
Enable warning state for channel.
.PARAMETER IsFloat
Specify the value is a float, instead of integer.
.PARAMETER ShowChart
Show the channel in the charts section of the web ui.
.PARAMETER ShowTable
Show the channel in the table section of the web ui.
.PARAMETER LimitMaxError
Set the maximum value before a channel goes into an error state. Only applies the first time a channel is reported to as sensor.
.PARAMETER LimitMinError
Set the minimum value before a channel goes into an error state. Only applies the first time a channel is reported to as sensor.
.PARAMETER LimitMaxWarning
Set the maximum value before a channel goes into a warning state. Only applies the first time a channel is reported to as sensor.
.PARAMETER LimitMinWarning
Set the minimum value before a channel goes into a warning state. Only applies the first time a channel is reported to as sensor.
.PARAMETER LimitErrorMsg
Set the message reported when the channel goes into an error state. Only applies the first time a channel is reported to as sensor.
.PARAMETER LimitMaxError
Set the message reported when the channel goes into a warning state. Only applies the first time a channel is reported to as sensor.
.PARAMETER LimitMode
Set if the Limits defined are active.
.PARAMETER ValueLookup
Set a custom lookup file for the channel.
#>
PARAM (
[Parameter(Mandatory=$True,Position=0)]
[string]$Channel,
[Parameter(Mandatory=$True,Position=1)]
[decimal]$Value,
[Parameter(Mandatory=$False)]
[string]$Unit,
[Parameter(Mandatory=$False)]
[Alias('ss')]
[ValidateSet("one","kilo","mega","giga","tera","byte","kilobyte","megabyte","gigabyte","terabyte","bit","kilobit","megabit","gigabit","terabit")]
[string]$SpeedSize,
[Parameter(Mandatory=$False)]
[Alias('vs')]
[ValidateSet("one","kilo","mega","giga","tera","byte","kilobyte","megabyte","gigabyte","terabyte","bit","kilobit","megabit","gigabit","terabit")]
[string]$VolumeSize,
[Parameter(Mandatory=$False)]
[Alias('st')]
[ValidateSet("second","minute","hour","day")]
[string]$SpeedTime,
[Parameter(Mandatory=$False)]
[switch]$Difference,
[Parameter(Mandatory=$False)]
[Alias('dm')]
[ValidateSet("auto","all")]
[string]$DecimalMode,
[Parameter(Mandatory=$False)]
[switch]$Warning,
[Parameter(Mandatory=$False)]
[switch]$IsFloat,
# note that both showchart and showtable default to "TRUE" in the actual API
# which is to say, if they're not defined, they're assumed to be true
# this is also true in the c# object that generates the XML,
# but it is NOT assumed to be true here.
# This is the part of the code that always puts in the showchart and showtables tags with zeroes!
[Parameter(Mandatory=$False)]
[Alias('sc')]
[switch]$ShowChart,
[Parameter(Mandatory=$False)]
#[Alias('st')] # also the alias to "speedtime"
[switch]$ShowTable,
[Parameter(Mandatory=$False)]
[int]$LimitMaxError = -1,
[Parameter(Mandatory=$False)]
[int]$LimitMinError = -1,
[Parameter(Mandatory=$False)]
[int]$LimitMaxWarning = -1,
[Parameter(Mandatory=$False)]
[int]$LimitMinWarning = -1,
[Parameter(Mandatory=$False)]
[string]$LimitErrorMsg,
[Parameter(Mandatory=$False)]
[string]$LimitWarningMsg,
#[Parameter(Mandatory=$False)]
#[Alias('lm')]
#[switch]$LimitMode,
[Parameter(Mandatory=$False)]
[Alias('vl')]
[string]$ValueLookup
)
BEGIN {
}