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
Add comment