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

PowerShell EXE/Script Advanced Sensor: Invalid JSON

Votes:

0

I'm trying to get a PowerShell script to work with an EXE/Script Advanced Sensor but I can't seem to get PRTG to recognise the JSON output. It says

"XML: The returned XML does not match the expected schema. (code: PE233) -- JSON: The returned JSON does not match the expected structure (Invalid JSON.). (code: PE231)"

If I run the script manually on the probe, it outputs good JSON which passes a validation test:

{
    "prtg": {
        "result": [
            {
                "channel": "Departments",
                "value": "0"
            },
            {
                "channel": "Home",
                "value": "0"
            },
            {
                "channel": "Shared",
                "value": "0"
            },
            {
                "channel": "Total",
                "value": "0"
            }
        ]
    }
}

I've enabled "Write EXE result to disk" but the log file simply contains 1,022 null characters which isn't very helpful.

I've seen other posts where the solution has been to use a 'here' string for the Write-Host cmdlet, but that hasn't helped.

This is the script:

# Inspired by these articles:
#   https://kb.paessler.com/en/topic/71662-how-to-monitor-dfs-replication-status
#   http://blog.simonw.se/programmatically-capture-verbose-output-in-a-powershell-variable/

# Requires the following Windows feature to be installed on the PRTG server:
#   Remote Server Administration Tools > Role Administration Tools > File Services Tools > DFS Management Tools

Param
(
    $SourceComputerName = $args[0],
    $DestinationComputerName = $args[1]
)

Import-Module DFSR

$DomainName = (Select-Object -InputObject $([System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()) -ExpandProperty Name)

Try
{
    $Folders = Get-DfsReplicatedFolder -DomainName $DomainName
}
Catch
{
    Write-Error -Message "Failed to query replication folders"
    Exit 1
}

$Count = @{}
$TotalCount = 0

foreach ($Folder in $Folders)
{
    Try
    {
        $VerboseMessage = $($Null = Get-DfsrBacklog -FolderName $Folder.FolderName -SourceComputerName $SourceComputerName -DestinationComputerName $DestinationComputerName -ErrorAction Stop -Verbose) 4>&1
    }
    Catch
    {
        Write-Error -Message "Failed to query backlog between $SourceComputerName and $DestinationComputerName"
        Exit 1
    }
    if ($VerboseMessage -Like "No backlog for the replicated folder named `"$($Folder.FolderName)`"")
    {
        $BacklogCount = 0
    }
    else
    {
        Try
        {
            $BacklogCount = [int]$($VerboseMessage -replace "The replicated folder has a backlog of files. Replicated folder: `"$($Folder.FolderName)`". Count: (\d+)",'$1')
        }
        Catch
        {
            Write-Warning -Message $_.Exception.Message
            Continue
        }
    }
    $Count.add($($Folder.FolderName), $BacklogCount)
    $TotalCount += $BacklogCount
}

Write-Host @"
{
    "prtg": {
        "result": [
            {
                "channel": "Departments",
                "value": "$($Count.Departments)"
            },
            {
                "channel": "Home",
                "value": "$($Count.Home)"
            },
            {
                "channel": "Shared",
                "value": "$($Count.Shared)"
            },
            {
                "channel": "Total",
                "value": "$TotalCount"
            }
        ]
    }
}
"@

I originally had the Write-Host output being produced within the foreach loop, but after reading about using a 'here' string, I changed it to what you see above, with all the Write-Host output being done at the end and with the channel/folder names hardcoded. Unfortunately it hasn't helped.

I created this test version of the script, which simply mocks up exactly the same output. This works fine with PRTG -- no invalid JSON errors:

$Count = @{
    Departments = 2
    Home = 3
    Shared = 4
}
$TotalCount = 9

Write-Host @"
{
    "prtg": {
        "result": [
            {
                "channel": "Departments",
                "value": "$($Count.Departments)"
            },
            {
                "channel": "Home",
                "value": "$($Count.Home)"
            },
            {
                "channel": "Shared",
                "value": "$($Count.Shared)"
            },
            {
                "channel": "Total",
                "value": "$TotalCount"
            }
        ]
    }
}
"@

Can someone please help me work out why PRTG doesn't like the JSON output from my first script?

Thanks - Simon

exe-script-advanced-sensor json prtg

Created on Jan 4, 2018 6:13:58 AM

Last change on Jan 4, 2018 6:48:39 AM by  Luciano Lingnau [Paessler]



1 Reply

Votes:

0

Hi Simon,

Please activate the "Write EXE result to disk" option in the sensor's settings and forward us the log files (Result of Sensor XXX.Data.txt and Result of Sensor XXX.txt) located on the corresponding probe under "C:\ProgramData\Paessler\PRTG Network Monitor\Logs (Sensors)". Please forward them by mail to "[email protected]". Please also include PAE976483 in the subject line.
This will help us to analyze the issue further.

Best regards.

Created on Jan 4, 2018 8:55:41 AM by  Dariusz Gorka [Paessler Support]




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.