What is this?

This knowledgebase contains questions and answers about PRTG Network Monitor and network monitoring in general.

Learn more

PRTG Network Monitor

Intuitive to Use. Easy to manage.
More than 500,000 users rely on Paessler PRTG every day. Find out how you can reduce cost, increase QoS and ease planning, as well.

Free Download

Top Tags


View all Tags

The correct way to build a PowerShell PRTG EXEXML sensor JSON output.

Votes:

3

I thought I should give back to the forum/community which has helped me a lot over the years. I've seen a lot of people writing PowerShell which builds text manually for JSON output. That's really fraught with danger in lots of ways.

The following PowerShell is an example of the _correct_ way to build a proper multidimensional object within PowerShell to pass to ConvertTo-JSON.

$jsonRoot = @{}
$jsonPrtgNode = @{}
$jsonResultsNode = @()

$resultHashTableItem = @{
    "channel"   = "Item Total"
    "unit"      = "Count"
    "mode"      = "Absolute"
    "showChart" = "1"
    "showTable" = "0"
    "warning"   = "0"
    "float"     = "0"
    "value"     = "123"
}
$jsonResultsNode += $resultHashTableItem

$resultHashTableItem = @{
    "channel"   = "Item Exit Total"
    "unit"      = "Count"
    "mode"      = "Absolute"
    "showChart" = "1"
    "showTable" = "0"
    "warning"   = "0"
    "float"     = "0"
    "value"     = "456"
}
$jsonResultsNode += $resultHashTableItem

$statusHashTableItem = @{
    "text"   = "Completed Sucessfully"
}
$jsonResultsNode += $statusHashTableItem

$jsonPrtgNode.Add("result",$jsonResultsNode)
$jsonRoot.Add("prtg",$jsonPrtgNode)

$jsonRoot | ConvertTo-Json -Depth 3

The output you will get from this example is the following

{
    "prtg":  {
                 "result":  [
                                {
                                    "channel":  "Item Total",
                                    "unit":  "Count",
                                    "showChart":  "1",
                                    "warning":  "0",
                                    "value":  "123",
                                    "mode":  "Absolute",
                                    "showTable":  "0",
                                    "float":  "0"
                                },
                                {
                                    "channel":  "Item Exit Total",
                                    "unit":  "Count",
                                    "showChart":  "1",
                                    "warning":  "0",
                                    "value":  "456",
                                    "mode":  "Absolute",
                                    "showTable":  "0",
                                    "float":  "0"
                                },
                                {
                                    "text":  "Completed Sucessfully"
                                }
                            ]
             }
}

I hope this helps someone avoid trying to build a large text/string output and makes their PowerShell JSON creation a lot more robust.

custom-sensor exe exexml json power powershell sensor shell xml

Created on Apr 7, 2022 5:53:26 AM

Last change on Apr 7, 2022 7:02:44 AM by  Luciano Lingnau [Paessler]



2 Replies

Votes:

0

Hello PRTG_Took_My_Life,
congrats on your first post and thank you for sharing!

In case someone wants a practical example, here are two scripts available in the KB that use a very similar approach for their output:

Best Regards,
Luciano Lingnau [Paessler]

Created on Apr 7, 2022 7:07:00 AM by  Luciano Lingnau [Paessler]



Votes:

0

I just needed this today and you saved my day, Thanks!

Created on Jan 11, 2023 8:46:57 AM




Disclaimer: The information in the Paessler Knowledge Base comes without warranty of any kind. Use at your own risk. Before applying any instructions please exercise proper system administrator housekeeping. You must make sure that a proper backup of all your data is available.