forked from yubu/psbbix-zabbix-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
psbbix.psm1
6476 lines (5796 loc) · 275 KB
/
psbbix.psm1
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
# Load additional libraries
Push-Location $psScriptRoot
. .\epoch-time-convert.ps1
. .\zabbix-db-size-calc.ps1
Pop-Location
function Remove-EmptyLines {
<#
.Synopsis
Remove empty lines from file, string or variable
.Description
Remove empty lines from file, string or variable
.Example
Remove-EmptyLines -in (gc c:\file.txt)
.Example
$var | Remove-EmptyLines
.Example
help -ex Remove-EmptyLines | Remove-EmptyLines
.Example
gc c:\*.txt | rmel
.Example
Get-ClipBoard | rmel
.Example
dir | oss | rmel
#>
[cmdletbinding()]
[Alias("rmel")]
param ([Parameter(Mandatory=$false,Position=0,ValueFromPipeline=$true)][array]$in)
process {
if (!$psboundparameters.count) {
help -ex $PSCmdlet.MyInvocation.MyCommand.Name | out-string | Remove-EmptyLines
return
}
$in.split("`r`n") | ? {$_.trim() -ne ""}
}
}
Function Write-MissingParamsMessage {
Write-Host "`nMissing parameters!`n" -f red
sleep 1
Get-Help -ex $PSCmdlet.MyInvocation.MyCommand.Name | out-string | Remove-EmptyLines
}
Function Get-ZabbixHelp {
<#
.Synopsis
Get fast help for most useful examples for every function with no empty lines
.Description
Get fast help for most useful examples for every function with no empty lines
.Example
Get-ZabbixHelp -list
Get list of all module functions, like gcm -module psbbix
.Example
Get-ZabbixHelp -alias
Get list of all aliases in the module
.Example
gzh host
Get examples for all zabbixhost commands
.Example
gzh hostinterface -p interface
Get all examples for Get/Set/New/Remove-ZabbixHostInterface with pattern "interface"
.Example
gzh hostinterface -p interface -short
Get all examples for Get/Set/New/Remove-ZabbixHostInterface with pattern "interface", print only matches
.Example
gzh host -p "copy|clone" -short
Get examples with copy or clone
.Example
gzh -zverb set
Get examples of all Set commands
.Example
gzh -zverb get
Get examples of all Get commands
.Example
gzh -zverb get hostinterface
Get examples for Get-ZabbixHostInterface
.Example
gzh user set
Get examples for Set-ZabbixUser
.Example
gzh host -p step
Find step by step guides
.Example
gzh item -p "cassandra|entropy"
Get help for cassandra items if you're using my cassandra cluster template
#>
[CmdletBinding()]
[Alias("gzh")]
Param ($znoun,$zverb,[switch]$list,$pattern,[switch]$short,[switch]$alias)
if (!(get-module "Find-String")) {Write-Host "`nInstall module Find-String from Powershell Gallery: install-module find-string -force. Unless this function won't work properly.`n" -f yellow; return }
if (!$psboundparameters.count) {Get-Help -ex $PSCmdlet.MyInvocation.MyCommand.Name | out-string | Remove-EmptyLines; return}
if ($list) {dir function:\*-zabbix* | select name | sort name}
elseif ($alias) {gcm -Module psbbix | %{gal -Definition $_.name -ea 0}}
elseif (!$znoun -and $pattern -and $short) {gzh | %{foreach ($i in $_) {$i | Select-String -Pattern $pattern -AllMatches | Out-ColorMatchInfo -onlyShowMatches}}}
elseif (!$znoun -and $pattern -and !$short) {gzh | out-string | Select-String -Pattern $pattern -AllMatches | Out-ColorMatchInfo -onlyShowMatches}
elseif ($znoun -and $pattern -and !$short) {gzh $znoun | out-string | Select-String -Pattern $pattern -AllMatches | Out-ColorMatchInfo -onlyShowMatches}
elseif ($znoun -and $pattern -and $short) {gzh $znoun | %{foreach ($i in $_) {$i | Select-String -Pattern $pattern -AllMatches | Out-ColorMatchInfo -onlyShowMatches}}}
elseif ($zverb -and !$znoun) {dir function:\$zverb-zabbix* | %{write-host $_.Name -f yellow; get-help -ex $_.Name | out-string | Remove-EmptyLines}}
elseif ($znoun -and !$zverb) {dir function:\*zabbix$znoun | %{write-host $_.Name -f yellow; get-help -ex $_.Name | out-string | Remove-EmptyLines}}
elseif ($zverb -and $znoun) {dir function:\$zverb-zabbix$znoun | %{write-host $_.Name -f yellow; get-help -ex $_.Name | out-string | Remove-EmptyLines}}
else {dir function:\*zabbix* | %{write-host $_.Name -f yellow; get-help -ex $_.Name | out-string | Remove-EmptyLines}}
}
Function New-ZabbixSession {
<#
.Synopsis
Create new Zabbix session
.Description
Create new Zabbix session
.Parameter PSCredential
Credential
.Parameter IPAddress
Accept IP address or domain name
.Parameter noSSL
Connect to Zabbix server with plain http
.Example
New-ZabbixSession 10.10.10.10
Connect to Zabbix server
.Example
Connect-Zabbix 10.10.10.10
Connect to Zabbix server
.Example
Connect-Zabbix -IPAddress 10.10.10.10 -noSSL
Connect to Zabbix server with noSSL (http)
.Example
Connect-Zabbix -User admin -Password zabbix -IPAddress zabbix.domain.net
Connect to Zabbix server
.Example
Connect-Zabbix -IPAddress zabbix.domain.net -URLCustomPath ""
Connect to Zabbix server with custom frontend install https://zabbix.domain.net, instead of default https://zabbix.domain.net/zabbix
#>
[CmdletBinding()]
[Alias("Connect-Zabbix","czab")]
Param (
[Parameter(Mandatory=$True)][string]$IPAddress,
[Parameter(Mandatory=$True)][PSCredential]$PSCredential,
[Parameter(Mandatory=$False)]$URLCustomPath="zabbix",
[Switch]$useSSL,
[switch]$noSSL
)
# if (!$psboundparameters.count) {Get-Help -ex $PSCmdlet.MyInvocation.MyCommand.Name | out-string | Remove-EmptyLines; return}
$Body = @{
jsonrpc = "2.0"
method = "user.login"
params = @{
user = $PSCredential.UserName
password = $PSCredential.GetNetworkCredential().Password
}
id = 1
auth = $null
}
$BodyJSON = ConvertTo-Json $Body
write-verbose $BodyJSON
if ($PSVersionTable.PSEdition -ne "core") {
if (!(test-connection $IPAddress -Quiet -Count 1)) {write-host "$IPAddress is not available.`n" -f red; return}
}
if ($noSSL) {
write-warning "You're going to connect via insecure HTTP protocol. Consider to use HTTPS."
$Protocol="http"
}
elseif ($useSSL) {
$Protocol="https"
}
else {
$Protocol="https"
}
# $URL = $Protocol+"://$IPAddress/zabbix"
$URL = $Protocol+"://$IPAddress/$URLCustomPath"
try {
if (!$global:zabSession -or !$global:zabSession.session) {
$global:zabSession=Invoke-RestMethod ("$URL/api_jsonrpc.php") -ContentType "application/json" -Body $BodyJSON -Method Post |
Select-Object jsonrpc,@{Name="session";Expression={$_.Result}},id,@{Name="URL";Expression={$URL}}
}
}
catch {
# [void]::$_
if ($_.exception -match "Unable to connect to the remote server") {write-host "`nNot connected! ERROR: $_`n" -f red; write-verbose $_.exception; return}
else {
write-host "`nSeems SSL certificate is self signed. Trying with no SSL validation..." -f yellow
if (($PSVersionTable.PSEdition -eq "core") -and !($PSDefaultParameterValues.keys -eq "Invoke-RestMethod:SkipCertificateCheck")) {$PSDefaultParameterValues.Add("Invoke-RestMethod:SkipCertificateCheck",$true)}
else {
write-host "`nWARNING: `nNo SSL validation setting in Windows Powershell is session wide.`nAs a result in this powershell session Invoke-Webrequest and Invoke-RestMethod may not work with regular websites: ex. iwr google.com`nUse new Powershell session for this purpose.`n" -f yellow
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
}
$global:zabSession=Invoke-RestMethod ("$URL/api_jsonrpc.php") -ContentType "application/json" -Body $BodyJSON -Method Post |
Select-Object jsonrpc,@{Name="session";Expression={$_.Result}},id,@{Name="URL";Expression={$URL}}
}
}
if ($zabSession.session) {
$global:zabSessionParams = [ordered]@{jsonrpc=$zabSession.jsonrpc;session=$zabSession.session;id=$zabSession.id;url=$zabSession.URL}
write-host "`nConnected to $IPAddress." -f green
write-host "Zabbix Server version: " -f green -nonewline
Get-ZabbixVersion
""
write-host 'Usage: Get-ZabbixHelp -list' -f yellow
write-host 'Usage: Get-ZabbixHelp -alias' -f yellow
""
}
else {
write-host "`nERROR: Not connected. Try again." -f red; $zabsession
if ($PSCredential.UserName -match "@|\\") {write-warning "`nYou have used domain user name format $($PSCredential.UserName). `nThis is not always supported in Zabbix configuration. Please ask your Zabbix admin for help.`n`n"}
}
}
Function Get-ZabbixSession {
<#
.Synopsis
Get Zabbix session
.Description
Get Zabbix session
.Example
Get-ZabbixSession
Get Zabbix session
.Example
Get-ZabbixConnection
Get Zabbix session
#>
[CmdletBinding()]
[Alias("Get-ZabbixConnection","gzconn","gzsess")]
param ()
if (!($global:zabSession -and $global:zabSessionParams)) {
write-host "`nDisconnected form Zabbix Server!`n" -f red; return
}
elseif ($global:zabSession -and $global:zabSessionParams -and !($ZabbixVersion=Get-ZabbixVersion)) {
write-host "`nZabbix session params are OK (use -verbose for details). Check whether Zabbix Server is online. In case of certificate error try new powershell session.`n" -f red; write-verbose "$($zabSession | select *)"; return
}
elseif ($global:zabSession -and $global:zabSessionParams -and ($ZabbixVersion=Get-ZabbixVersion)) {
$zabSession | select *, @{n="ZabbixVer";e={$ZabbixVersion}}
}
else {write-host "`nDisconnected form Zabbix Server!`n" -f red; return}
}
Function Remove-ZabbixSession {
<#
.Synopsis
Remove Zabbix session
.Description
Remove Zabbix session
.Example
Disconnect-Zabbix
Disconnect from Zabbix server
.Example
Remove-Zabbixsession
Disconnect from Zabbix server
#>
[CmdletBinding()]
[Alias("Disconnect-Zabbix","rzsess","dzsess")]
Param (
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$jsonrpc=($global:zabSessionParams.jsonrpc),
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$session=($global:zabSessionParams.session),
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$id=($global:zabSessionParams.id),
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$URL=($global:zabSessionParams.url)
)
# if (!$psboundparameters.count -and !$global:zabSessionParams) {Get-Help -ex $PSCmdlet.MyInvocation.MyCommand.Name | out-string | Remove-EmptyLines; return}
if (!$psboundparameters.count -and !$global:zabSessionParams) {Write-Host "`nDisconnected from Zabbix Server!`n" -f red; return}
if (Get-ZabbixSession) {
$Body = @{
method = "user.logout"
jsonrpc = $jsonrpc
params = @{}
id = $id
auth = $session
}
$BodyJSON = ConvertTo-Json $Body
write-verbose $BodyJSON
$a = Invoke-RestMethod "$URL/api_jsonrpc.php" -ContentType "application/json" -Body $BodyJSON -Method Post
if ($a.result) {$a.result | out-null} else {$a.error}
$global:zabSession = ""
$global:zabSessionParams = ""
if (!(Get-ZabbixVersion)) {}
}
else {Get-ZabbixSession}
}
Function Get-ZabbixVersion {
<#
.Synopsis
Get Zabbix server version
.Description
Get Zabbix server version
.Example
Get-ZabbixVersion
Get Zabbix server version
.Example
Get-ZabbixVersion
Get Zabbix server version
#>
[CmdletBinding()]
[Alias("gzver")]
Param (
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][array]$params=@(),
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$jsonrpc=($global:zabSessionParams.jsonrpc),
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$session=($global:zabSessionParams.session),
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$id=($global:zabSessionParams.id),
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$URL=($global:zabSessionParams.url)
)
if (!($global:zabSession -or $global:zabSessionParams)) {write-host "`nDisconnected from Zabbix Server!`n" -f red; return}
else {
$Body = @{
method = "apiinfo.version"
jsonrpc = $jsonrpc
id = $id
params = $params
}
$BodyJSON = ConvertTo-Json $Body
write-verbose $BodyJSON
try {
$a = Invoke-RestMethod "$URL/api_jsonrpc.php" -ContentType "application/json" -Body $BodyJSON -Method Post
if ($a.result) {return $a.result} else {$a.error}
}
catch {Write-Host "`nERROR: $_." -f red}
}
}
Function Get-ZabbixHost {
<#
.Synopsis
Get hosts
.Description
Get hosts
.Parameter HostName
To filter by HostName of the host (case sensitive)
.Parameter HostID
To filter by HostID of the host
.Example
Get-ZabbixHost
Get all hosts
.Example
Get-ZabbixHost -HostName SomeHost
Get host by name (case sensitive)
.Example
Get-ZabbixHost | ? name -match host | select hostid,host,status,available,httptests
Get host(s) by name match (case insensitive)
.Example
Get-ZabbixHost | ? name -match host | select -ExpandProperty interfaces -Property name | sort name
Get hosts' interfaces by host name match (case insensitive)
.Example
Get-ZabbixHost | ? name -match host | Get-ZabbixTemplate | select templateid,name -Unique
Get templates by name match (case insensitive)
.Example
Get-ZabbixHost | ? status -eq 1 | select hostid,name
Get only disabled hosts
.Example
Get-ZabbixHost -sortby name | ? name -match host | select hostid,host,status -ExpandProperty httptests
Get host(s) by name match (case insensitive), sort by name. Possible values are: hostid, host, name (default), status
.Example
Get-ZabbixHost | ? name -match HostName | select name,*error* | ft -a
Get all errors for hosts
.Example
Get-ZabbixHost | ? name -match HostName | select name,*jmx* | ft -a
Get info regarding JMX connections for hosts
.Example
Get-ZabbixHost | ? name -match "" | ? jmx_available -match 1 | select hostid,name,jmx_available
Get host(s) with JMX interface(s) active
.Example
Get-ZabbixHost | ? parentTemplates -match "jmx" | select hostid,name,available,jmx_available
Get host(s) with JMX Templates and get their connection status
.Example
Get-ZabbixHost | ? status -eq 0 | ? available -eq 0 | select hostid,name,status,available,jmx_available | ft -a
Get hosts, which are enabled, but unreachable
.Example
Get-ZabbixHost -GroupID (Get-ZabbixGroup -GroupName "groupName").groupid | ? httpTests | select hostid,host,status,available,httptests | sort host | ft -a
Get host(s) by host group, match name "GroupName" (case sensitive)
.Example
Get-ZabbixHost -hostname HostName | Get-ZabbixItem -WebItems -ItemKey web.test.error -ea silent | select name,key_,lastclock
Get web tests items for the host (HostName is case sensitive)
.Example
(Get-ZabbixHost | ? name -match host).parentTemplates.name
Get templates, linked to the host by hostname match (case insensitive)
.Example
Get-ZabbixHost | ? name -match hostName | select host -ExpandProperty parentTemplates
Get templates, linked to the host(s)
.Example
Get-ZabbixHost | ? parentTemplates -match "jmx" | select name -Unique
Get hosts with templates, by template name match
.Example
Get-ZabbixHost -HostName HostName | Get-ZabbixItem -WebItems -ItemKey web.test.error -ea silent | select name,key_,@{n='lastclock';e={convertFrom-epoch $_.lastclock}}
Get Items for the host. Item lastclock (last time it happened in UTC)
.Example
Get-ZabbixHost -hostname HostName | Get-ZabbixHttpTest -ea silent | select httptestid,name,steps
Get host (case sensitive) and it's HttpTests
.Example
Get-ZabbixHost -hostname HostName | Get-ZabbixHttpTest -ea silent | select -ExpandProperty steps | ft -a
Get host (case sensitive) and it's HttpTests
.Example
Get-ZabbixHost | ? name -match hostName | select host -ExpandProperty interfaces | ? port -match 10050
Get interfaces for the host(s)
.Example
Get-ZabbixHost | ? name -match hostname | Get-ZabbixHostInterface | ? port -match 10050 | ft -a
Get interfaces for the host(s)
.Example
Get-ZabbixHost | ? name -match hostsName | %{$n=$_.name; Get-ZabbixHostInterface -HostID $_.hostid} | select @{n="name";e={$n}},hostid,interfaceid,ip,port | sort name | ft -a
Get interface(s) for the host(s)
#>
[CmdletBinding()]
[Alias("gzhst")]
Param (
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$False)]$HostName,
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$False)][array]$HostID,
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$False)][array]$GroupID,
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$False)][array]$HttpTestID,
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$False)][string]$SortBy="name",
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$True)][string]$status,
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$jsonrpc=($global:zabSessionParams.jsonrpc),
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$session=($global:zabSessionParams.session),
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$id=($global:zabSessionParams.id),
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$URL=($global:zabSessionParams.url)
)
process {
if (!(Get-ZabbixSession)) {return}
# if (!$psboundparameters.count -and !$global:zabSessionParams) {Get-Help -ex $PSCmdlet.MyInvocation.MyCommand.Name | out-string | Remove-EmptyLines; return}
$boundparams=$PSBoundParameters | out-string
write-verbose "($boundparams)"
$Body = @{
method = "host.get"
params = @{
output = "extend"
selectGroups = @(
"groupid",
"name"
)
selectParentTemplates = @(
"templateid",
"name"
)
selectInterfaces = @(
"interfaceid",
"ip",
"port"
)
selectHttpTests = @(
"httptestid",
"name",
"steps"
)
selectTriggers = @(
"triggerid",
"description"
)
selectApplications = @(
"applicationid"
"name"
)
selectGraphs = @(
"graphid"
"name"
)
selectMacros = @(
"hostmacroid"
"macro"
"value"
)
selectScreens = @(
"screenid"
"name"
)
selectInventory = @(
"name"
"type"
"os"
)
hostids = $HostID
groupids = $GroupID
httptestid = $HttpTestID
filter = @{
host = $HostName
}
sortfield = $SortBy
}
jsonrpc = $jsonrpc
id = $id
auth = $session
}
$BodyJSON = ConvertTo-Json $Body
write-verbose $BodyJSON
try {
$a = Invoke-RestMethod "$URL/api_jsonrpc.php" -ContentType "application/json" -Body $BodyJSON -Method Post
if ($a.result) {$a.result} else {$a.error}
} catch {
Write-Host "$_"
Write-Host "Too many entries to return from Zabbix server. Check/reduce the filters." -f cyan
}
}
}
Function Set-ZabbixHost {
<#
.Synopsis
Set/update host settings
.Description
Set/update host settings
.Parameter HostID
HostID
.Parameter HostName
Host name
.Parameter HostVisibleName
Host visible name: Default: host property value
.Parameter HostDescription
Description of the host
.Parameter status
Status and function of the host: Possible values are: 0 - (default) monitored host; 1 - unmonitored host
.Parameter InventoryMode
InventoryMode: Possible values are: -1 - disabled; 0 - (default) manual; 1 - automatic
.Parameter IpmiAuthtype
IPMI: IpmiAuthtype: IPMI authentication algorithm: Possible values are: -1 - (default) default; 0 - none; 1 - MD2; 2 - MD5 4 - straight; 5 - OEM; 6 - RMCP+
.Parameter IpmiUsername
IPMI: IpmiUsername: IPMI username
.Parameter IpmiPassword
IPMI: IpmiPassword: IPMI password
.Parameter IpmiPrivilege
IPMI: IpmiPrivilege: IPMI privilege level: Possible values are: 1 - callback; 2 - (default) user; 3 - operator; 4 - admin; 5 - OEM
.Parameter GroupID
GroupID of host group
.Example
Get-ZabbixHost | ? name -eq "host" | Set-ZabbixHost -status 0
Enable host (-status 0)
.Example
(1..9) | %{(Get-ZabbixHost | ? name -eq "host0$_") | Set-ZabbixHost -status 1}
Disable multiple hosts (-status 1)
.Example
Get-ZabbixHost | ? name -match "hostName" | Set-ZabbixHost -status 0
Enable multiple hosts
.Example
Get-ZabbixHost | ? name -match hostName | Set-ZabbixHost -GroupID 14,16 -status 0
Set HostGroups for the host(s) and enable it
.Example
Get-ZabbixHost | ? name -eq hostName | Set-ZabbixHost -removeTemplates -WhatIf
WhatIf on delete and clear of all templates from the host
.Example
Get-ZabbixHost | ? name -eq hostName | Set-ZabbixHost -removeTemplates
Remove and clear all linked templates from the host
.Example
Get-ZabbixHost | ? name -eq hostName | Set-ZabbixHost -TemplateID (Get-ZabbixTemplate | ? name -eq "TemplateFromCurrentHost").templateid
Replace linked templates on the host with new ones (not clear the old ones)
.Example
Get-ZabbixHost | ? name -eq hostName | Set-ZabbixHost -removeTemplates -TemplateID (Get-ZabbixTemplate | ? name -eq "TemplateFromCurrentHost").templateid -WhatIf
WhatIf on remove and clear linked templates from the host
.Example
Get-ZabbixHost -HostName HostName | Set-ZabbixHost -removeTemplates -TemplateID (Get-ZabbixHost -HostName "HostName").parentTemplates.templateid
Unlink(remove) and clear templates from the host (case sensitive)
.Example
$templateID=(Get-ZabbixTemplate -HostID (Get-ZabbixHost | ? name -match hostname).hostid).templateid
Store existing templateIDs
$templateID+=(Get-ZabbixTemplate | ? name -match "newTemplate").templateid
Add new templateIDs
Get-ZabbixHost | ? name -match hosts | Set-ZabbixHost -TemplateID $templateID
Link(add) additional template(s) to already existing ones, step by step
.Example
$templateID=((Get-ZabbixHost | ? name -eq hostName).parentTemplates.templateid)+((Get-ZabbixTemplate | ? name -match mysql).templateid)
Get-ZabbixHost | ? name -ew hostName | Set-ZabbixHost -TemplateID $templateID
Add new template to existing ones (not replace)
.Example
Get-ZabbixHost -HostName HostName | Set-ZabbixHost -TemplateID (Get-ZabbixHost -HostName SourceHost).parentTemplates.templateid
Link(replace existing) new templates to the host, according config of other host (case sensitive)
.Example
(1..9) | %{Get-ZabbixHost -HostName "Host0$_" | Set-ZabbixHost -TemplateID ((Get-ZabbixHost | ? name -match "sourcehost").parenttemplates.templateid)}
Link(replace existing) new templates to multiple hosts, according config of the other host
#>
[CmdletBinding(SupportsShouldProcess,ConfirmImpact='High')]
[Alias("szhst")]
Param (
[Alias("host")][Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$True)][string]$HostName,
# Visible name of the host. Default: host property value.
[Alias("name")][Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$True)][string]$HostVisibleName,
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$True)][string]$HostID,
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$False)][array]$GroupID,
[Parameter(DontShow,Mandatory=$False,ValueFromPipelineByPropertyName=$True)][array]$groups,
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$False)][array]$interfaceID,
[Parameter(DontShow,Mandatory=$False,ValueFromPipelineByPropertyName=$True)][array]$interfaces,
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$False)][array]$TemplateID,
[Parameter(DontShow,Mandatory=$False,ValueFromPipelineByPropertyName=$True)][array]$parentTemplates,
# [Alias("parentTemplates")][Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$True)][array]$templates,
[Parameter(DontShow,Mandatory=$False,ValueFromPipelineByPropertyName=$True)][array]$templates,
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$True)]$Inventory,
[Alias("description")][Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$True)][string]$HostDescription,
# Host inventory population mode: Possible values are: -1 - disabled; 0 - (default) manual; 1 - automatic.
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$True)][int]$InventoryMode,
# IPMI authentication algorithm: Possible values are: -1 - (default) default; 0 - none; 1 - MD2; 2 - MD5; 4 - straight; 5 - OEM; 6 - RMCP+
[Alias("ipmi_authtype")][Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$True)][int]$IpmiAuthtype,
[Alias("ipmi_username")][Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$True)][string]$IpmiUsername,
[Alias("ipmi_password")][Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$True)][string]$IpmiPassword,
# IPMI privilege level: Possible values are: 1 - callback; 2 - (default) user; 3 - operator; 4 - admin; 5 - OEM.
[Alias("ipmi_privilege")][Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$True)][int]$IpmiPrivilege,
# [array]$GroupID,
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$True)][array]$HttpTestID,
# Status and function of the host: Possible values are: 0 - (default) monitored host; 1 - unmonitored host
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$True)][int]$status,
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$True)][int]$ProxyHostID,
[switch]$removeTemplates,
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$jsonrpc=($global:zabSessionParams.jsonrpc),
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$session=($global:zabSessionParams.session),
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$id=($global:zabSessionParams.id),
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$URL=($global:zabSessionParams.url)
)
process {
if (!(Get-ZabbixSession)) {return}
elseif (!$psboundparameters.count) {Write-MissingParamsMessage; return}
$boundparams=$PSBoundParameters | out-string
write-verbose "($boundparams)"
if ($TemplateID -eq 0) {$TemplateID=""}
# if ($TemplateID.count -gt 9) {write-host "`nOnly up to 5 templates are allowed." -f red -b yellow; return}
for ($i=0; $i -lt $TemplateID.length; $i++) {[array]$tmpl+=$(@{templateid = $($TemplateID[$i])})}
for ($i=0; $i -lt $GroupID.length; $i++) {[array]$grp+=$(@{groupid = $($GroupID[$i])})}
for ($i=0; $i -lt $interfaceID.length; $i++) {[array]$ifc+=$(@{interfaceid = $($interfaceID[$i])})}
$Body = @{
method = "host.update"
params = @{
hostid = $HostID
status = $status
host = $HostName
name = $HostVisibleName
ipmi_authtype = $IpmiAuthtype
ipmi_username = $IpmiUsername
ipmi_password = $IpmiPassword
ipmi_privilege = $IpmiPrivilege
description = $HostDescription
inventory_mode = $InventoryMode
proxy_hostid = $ProxyHostID
}
jsonrpc = $jsonrpc
id = $id
auth = $session
}
if (!$TemplateID -and $removeTemplates) {$Body.params.templates_clear=$parentTemplates | select templateid}
if ($TemplateID -and $removeTemplates) {$Body.params.templates_clear=$tmpl | ?{$_}}
if ($TemplateID -and !$removeTemplates) {$Body.params.templates=$tmpl | ?{$_}}
# if (!($TemplateID -and $removeTemplates)) {$Body.params.parenttemplates=$parentTemplates}
if ($GroupID) {$Body.params.groups=$grp} else {$Body.params.groups=$groups}
if ($interfaceID) {$Body.params.interfaces=$ifc} else {$Body.params.interfaces=($interfaces)}
$BodyJSON = ConvertTo-Json $Body -Depth 3
write-verbose $BodyJSON
if (!$removeTemplates) {
$a = Invoke-RestMethod "$URL/api_jsonrpc.php" -ContentType "application/json" -Body $BodyJSON -Method Post
if ($a.result) {$a.result} else {$a.error}
}
elseif ($removeTemplates -and !$TemplateID) {
if ([bool]$WhatIfPreference.IsPresent) {}
if ($PSCmdlet.ShouldProcess((($parentTemplates).name -join ", "),"Unlink and clear templates from the host")) {
$a = Invoke-RestMethod "$URL/api_jsonrpc.php" -ContentType "application/json" -Body $BodyJSON -Method Post
if ($a.result) {$a.result} else {$a.error}
}
}
elseif ($removeTemplates -and $TemplateID) {
if ([bool]$WhatIfPreference.IsPresent) {}
if ($PSCmdlet.ShouldProcess(($parentTemplates | ? templateid -match (($tmpl).templateid -join "|")).name,"Unlink and clear templates")) {
$a = Invoke-RestMethod "$URL/api_jsonrpc.php" -ContentType "application/json" -Body $BodyJSON -Method Post
if ($a.result) {$a.result} else {$a.error}
}
}
}
}
Function New-ZabbixHost {
<#
.Synopsis
Create new host
.Description
Create new host
.Parameter HostName
HostName of the host as it will appear in zabbix
.Parameter IP
IP address of the host
.Parameter DNSName
Domain name of the host
.Parameter Port
Port to connect to the host (default 10050)
.Parameter GroupID
ID of the group host will belong to
.Parameter TemplateID
ID/IDs of the templates to link to the host
.Parameter MonitorByDNSName
If used, domain name of the host will used to connect
.Example
New-ZabbixHost -HostName NewHost -IP 10.20.10.10 -GroupID 8,14 -TemplateID "10081","10166"
Create new host (case sensitive), with two linked Templates and member of two HostGroups
.Example
New-ZabbixHost -HostName hostName -IP 10.20.10.10 -TemplateID (Get-ZabbixTemplate | ? name -eq "Template OS Windows").templateid -GroupID (Get-ZabbixGroup | ? name -eq "HostGroup").groupid -status 1
Create new host (case sensitive), with one linked Template, member of one HostGroup and disabled (-status 1)
.Example
New-ZabbixHost -HostName NewHost -IP 10.20.10.10 -TemplateID ((Get-ZabbixTemplate | ? name -match "Template OS Windows|Template OS Windows - Ext") -notmatch "Template OS Windows - Backup").templateid -GroupID (Get-ZabbixHostGroup | ? name -match "HostGroup1|HostGroup2").groupid -status 1
Create new host (case sensitive), with two linked Templates, member of two HostGroups and disabled (-status 1)
.Example
New-ZabbixHost -HostName NewHost -IP 10.20.10.10 -GroupID 8 -TemplateID (Get-ZabbixHost | ? name -match "host").parentTemplates.templateid -status 0
Create new host (case sensitive), with multiple attached Templates and enable it (-status 0)
.Example
New-ZabbixHost -HostName NewHost -IP 10.20.10.10 -GroupID 8 -TemplateID (Get-ZabbixHost | ? name -match "host").parentTemplates.templateid -status 1
Create new host (case sensitive), with multiple attached Templates and leave it disabled (-status 1)
.Example
New-ZabbixHost -HostName NewHost -IP 10.20.10.10 -TemplateID ((Get-ZabbixTemplate | ? name -match "Template OS Windows") -notmatch "Template OS Windows - Backup").templateid -verbose -GroupID (Get-ZabbixGroup | ? name -match "HostGroup1|HostGroup2").groupid -status 1 -DNSName NewHost.example.com -MonitorByDNSName
Create new host with FQDN and set monitoring according DNS and not IP
.Example
Import-Csv c:\new-servers.csv | %{New-ZabbixHost -HostName $_.$Hostname -IP $_.IP -TemplateID "10081","10166" -GroupID 8,14}
Mass create new hosts
.Example
Import-Csv c:\new-servers.csv | %{New-ZabbixHost -HostName $_.Hostname -IP $_.IP -GroupID $_.GroupID -TemplateID $_.TemplateID -status $_.status}
Mass create new hosts
.Example
New-ZabbixHost -HostName NewHost -IP 10.20.10.10 -GroupID (Get-ZabbixHost | ? name -eq "SourceHost").groups.groupid -TemplateID (Get-ZabbixHost | ? name -eq "SourceHost" | Get-ZabbixTemplate | ? name -match os).templateid -status 1 -verbose
Clone HostGroups and Templates from other host
.Example
Get-ZabbixHost | ? name -match SourceHost | New-ZabbixHost -HostName NewHost -IP 10.20.10.10 -status 1
Partially clone host (templates and groups will be copied from the source host)
.Example
Get-ZabbixHost | ? name -eq SourceHost | Get-ZabbixApplication | New-ZabbixApplication -HostID (Get-ZabbixHost | ? name -match newHost).hostid
Clone Application(s) from host to host
#>
[CmdletBinding()]
[Alias("nzhst")]
Param (
[Parameter(Mandatory=$True)][string]$HostName,
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$false)][string]$IP,
[string]$DNSName,
[Switch]$MonitorByDNSName,
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$True)][string]$Port = 10050,
[Parameter(Mandatory=$True,ValueFromPipelineByPropertyName=$True)][string]$status,
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$False)][array]$GroupID,
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$True)][array]$groups,
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$False)][array]$TemplateID,
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$False)][int]$ProxyHostID,
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$True)][array]$templates,
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$False)][array]$interfaces,
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$True)][array]$parentTemplates,
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$True)][string]$jsonrpc=($global:zabSessionParams.jsonrpc),
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$True)][string]$session=($global:zabSessionParams.session),
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$True)][string]$id=($global:zabSessionParams.id),
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$True)][string]$URL=($global:zabSessionParams.url)
)
process {
if (!(Get-ZabbixSession)) {return}
elseif (!$psboundparameters.count) {Write-MissingParamsMessage; return}
# if (!$psboundparameters.count -and !$global:zabSessionParams) {Get-Help -ex $PSCmdlet.MyInvocation.MyCommand.Name | out-string | Remove-EmptyLines; return}
$boundparams=$PSBoundParameters | out-string
write-verbose "($boundparams)"
Switch ($MonitorByDNSName.IsPresent) {
$False {$ByDNSName = 1} # = ByIP
$True {$ByDNSName = 0} # = ByDomainName
}
for ($i=0; $i -lt $TemplateID.length; $i++) {[array]$tmpl+=$(@{templateid = $($TemplateID[$i])})}
for ($i=0; $i -lt $GroupID.length; $i++) {[array]$grp+=$(@{groupid = $($GroupID[$i])})}
$Body = @{
method = "host.create"
params = @{
host = $HostName
interfaces = @{
type = 1
main = 1
useip = $ByDNSName
ip = $IP
dns = $DNSName
port = $Port
}
status = $Status
proxy_hostid = $ProxyHostID
}
jsonrpc = $jsonrpc
auth = $session
id = $id
}
if ($GroupID) {$Body.params.groups=$grp} elseif ($groups) {$Body.params.groups=$groups}
if ($TemplateID -and ($TemplateID -ne 0)) {$Body.params.templates=$tmpl} elseif ($parentTemplates) {$Body.params.templates=$parentTemplates | select templateid}
$BodyJSON = ConvertTo-Json $Body -Depth 3
write-verbose $BodyJSON
$a = Invoke-RestMethod "$URL/api_jsonrpc.php" -ContentType "application/json" -Body $BodyJSON -Method Post
if ($a.result) {
$a.result
if ($psboundparameters.interfaces) {
write-host "Replacing the IP address in cloned interfaces..." -f green
Get-ZabbixHost -HostName $HostName | Get-ZabbixHostInterface | %{Set-ZabbixHostInterface -InterfaceID $_.interfaceid -IP $IP -Port $_.port -HostID $_.hostid -main $_.main}
}
} else {$a.error}
}
}
Function Remove-ZabbixHost {
<#
.Synopsis
Delete/Remove selected host
.Description
Delete/Remove selected host
.Parameter HostID
To filter by ID/IDs
.Example
Remove-ZabbixHost -HostID (Get-ZabbixHost | ? name -match "RetiredHosts").hostid -WhatIf
Remove host(s) by name match (case insensitive) (check only: -WhatIf)
.Example
Remove-ZabbixHost -HostID (Get-ZabbixHost | ? name -match "RetiredHosts").hostid
Remove host(s) by name match (case insensitive)
.Example
Remove-ZabbixHost -HostID "10001","10002"
Remove hosts by IDs
.Example
Remove-ZabbixHost -HostID (Get-ZabbixHost -HostName HostRetired).hostid
Remove single host by name (exact match, case sensitive)
.Example
Get-ZabbixHost | ? name -eq HostName | Remove-ZabbixHost -WhatIf
Remove hosts (check only: -WhatIf)
.Example
Get-ZabbixHost | ? name -eq HostName | Remove-ZabbixHost
Remove host
.Example
Get-ZabbixHost | ? name -match HostName0[1-8] | Remove-ZabbixHost
Remove multiple hosts
.Example
Get-ZabbixHost | Remove-ZabbixHost
Will delete ALL hosts from Zabbix
#>
[CmdletBinding(SupportsShouldProcess,ConfirmImpact='High')]
[Alias("Delete-ZabbixHost","rzhst","dzhst")]
Param (
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][array]$HostID,
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$Name,
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$jsonrpc=($global:zabSessionParams.jsonrpc),
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$session=($global:zabSessionParams.session),
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$id=($global:zabSessionParams.id),
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$URL=($global:zabSessionParams.url)
)
process {
if (!(Get-ZabbixSession)) {return}
elseif (!$psboundparameters.count) {Write-MissingParamsMessage; return}
$boundparams=$PSBoundParameters | out-string
write-verbose "($boundparams)"
$Body = @{
method = "host.delete"
params = @($HostID)
jsonrpc = $jsonrpc
id = $id
auth = $session
}
$BodyJSON = ConvertTo-Json $Body
write-verbose $BodyJSON
if ([bool]$WhatIfPreference.IsPresent) {}
if ($PSCmdlet.ShouldProcess($Name,"Delete")) {
$a = Invoke-RestMethod "$URL/api_jsonrpc.php" -ContentType "application/json" -Body $BodyJSON -Method Post
}
if ($a.result) {$a.result} else {$a.error}
}
}
Function Copy-ZabbixHost {
<#
.Synopsis
Copy/clone host
.Description
Copy/clone host
.Parameter HostName
HostName of the host as it will display on zabbix
.Parameter IP
IP address to supervise the host
.Parameter DNSName
Domain name to supervise the host
.Parameter Port
Port to supervise the host
.Parameter GroupID
ID of the group where add the host
.Parameter TemplateID
ID/IDs of the templates to add to the host
.Parameter MonitorByDNSName
If used, domain name of the host will used to contact it
.Example
Get-ZabbixHost -HostName SourceHost | Copy-ZabbixHost -HostName NewHost -IP 10.20.10.10
Full copy of the host with new Hostname and IP
.Example
Get-ZabbixHost | ? name -eq sourceHost | Copy-ZabbixHost -HostName NewHost -IP 10.20.10.10
Full clone of the host with new Hostname and IP
.Example
Get-ZabbixHost | ? name -eq SourceHost | Copy-ZabbixHost -HostName NewHost -IP 10.20.10.10 -status 1
Full clone of the host with new Hostname and IP with status 1 (disabled)
.Example
Import-Csv c:\new-servers.csv | %{Get-ZabbixHost | ? name -eq SourceHost | Clone-ZabbixHost -HostName $_.Hostname -IP $_.IP}
Mass clone new hosts
#>
[CmdletBinding()]
[Alias("Clone-ZabbixHost","czhst")]
Param (
[Parameter(Mandatory=$True,ValueFromPipelineByPropertyName=$false)][string]$HostName,
[Parameter(DontShow,Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$HostID,
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$false)][array]$TemplateID,
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$false)][string]$IP,
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$Port = 10050,
[Parameter(DontShow,Mandatory=$False,ValueFromPipelineByPropertyName=$true)][array]$interfaces,
[Alias("parentTemplates")][Parameter(DontShow,Mandatory=$False,ValueFromPipelineByPropertyName=$true)][array]$templates,
[Parameter(Mandatory=$True,ValueFromPipelineByPropertyName=$true)][string]$status,
[Parameter(DontShow,Mandatory=$False,ValueFromPipelineByPropertyName=$true)][array]$groups,
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$false)][string]$GroupID,
[Parameter(DontShow,Mandatory=$False,ValueFromPipelineByPropertyName=$true)][array]$httpTests,
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$false)][int]$ProxyHostID,
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$jsonrpc=($global:zabSessionParams.jsonrpc),
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$session=($global:zabSessionParams.session),
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$id=($global:zabSessionParams.id),
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)][string]$URL=($global:zabSessionParams.url)
)
process {
if (!(Get-ZabbixSession)) {return}
elseif (!$psboundparameters.count) {Write-MissingParamsMessage; return}
$boundparams=$PSBoundParameters | out-string
write-verbose "($boundparams)"
$Body = @{
method = "host.create"
params = @{
host = $HostName
# interfaces = $interfaces
interfaces = (Get-ZabbixHostInterface -HostID $hostid | select * -ExcludeProperty hostid,interfaceid,bulk)
templates = ($templates | select templateid)
groups = ($groups | select groupid)
# proxy_hostid = $ProxyHostID
status = $Status
}
jsonrpc = $jsonrpc
auth = $session
id = $id
}
# if ($IP) {$Body.params.interfaces = }
if ($httpTests) {$Body.params.httptests = ($httpTests | select httptestid)}
if ($ProxyHostID) {$Body.params.proxy_hostid = $ProxyHostID}
$BodyJSON = ConvertTo-Json $Body -Depth 3
write-verbose $BodyJSON
$a = Invoke-RestMethod "$URL/api_jsonrpc.php" -ContentType "application/json" -Body $BodyJSON -Method Post
#$a.result.hostids
if ($a.result) {
$a.result
write-verbose " --> Going to replace the IP address in cloned interfaces..."
Get-ZabbixHost -HostName $HostName | Get-ZabbixHostInterface | Set-ZabbixHostInterface -IP $IP
} else {$a.error}
}
}