forked from beatcracker/Powershell-Misc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConvertTo-ZabbixJson.ps1
88 lines (73 loc) · 2.51 KB
/
ConvertTo-ZabbixJson.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
<#
.Synopsis
Convert object to JSON supported by Zabbix low-level discovery.
.Description
Convert object to JSON supported by Zabbix low-level discovery.
Object property names will be converted to uppercase, prefixed by #
and wrapped in curly braces: Name -> {#NAME}
.Parameter InputObject
Object to be converted. You can supply multiple objects.
.Parameter Compress
Omits white space and indented formatting in the output string.
If not set, the ConvertTo-Json defaults are used.
.Example
[pscustomobject]@{a=1 ; b = 2}, @{c = 3 ; d = 4} | ConvertTo-ZabbixJson
Converts PSCustomObject and hashtable to Zabbix LLD JSON:
{
"data": [
{
"{#A}": 1,
"{#B}": 2
},
{
"{#D}": 4,
"{#C}": 3
}
]
}
.Example
Get-Website | Select-Object name, physicalPath | ConvertTo-ZabbixJson
Converts website object to Zabbix LLD JSON:
{
"data": [
{
"{#NAME}": "Default Web Site",
"{#PHYSICALPATH}": "C:\\inetpub\\wwwroot"
}
]
}
#>
function ConvertTo-ZabbixJson {
[CmdletBinding()]
Param (
[Parameter(ValueFromPipeline = $true, Position = 0)]
$InputObject,
[Parameter(Position = 1)]
[switch]$Compress
)
Begin {
$Result = New-Object -TypeName System.Collections.Generic.List[PSCustomObject]
}
Process {
foreach ($item in $InputObject) {
# if item is hashtable, convert it to PSCustomObject
$item = [pscustomobject]$item
if ($InvalidPropertyName = @($item.PsObject.Properties.Name) -match '[^0-9A-Z_\.]') {
throw "Invalid property name: $InvalidPropertyName . Allowed symbols for LLD macro names are: 0-9 , A-Z , _ , ."
}
# Build properties for Zabbix LLD object
foreach ($property in $item.PsObject.Properties.Name) {
[void]$Result.Add(
@{
"{#$($property.ToUpperInvariant())}" = $item.$property.ToString()
}
)
}
}
}
End {
if ($Result.Count) {
@{data = $Result} | ConvertTo-Json -Compress:$Compress
}
}
}