We've written this script to extract some counter data from our software, The output looks to be formatted correctly but we are still getting the "XML: Junk after document element </prtg> -- JSON: The returned JSON does not match the expected structure (Invalid JSON.). (code: PE231)" error.
The values shown in the output are static, just trying to get something into prtg for now.
Below is the script and output.
Script
$baseURL = "http://ourservice/api/v1/" $view = "ourservice" $userName = "" $password = "" $application = "PRTG" #$tagsToRead = @(($view + ".{Diagnostics}.Reading.TagHandles"); ($view + ".{Diagnostics}.Reading.NumClients")) $tagsToRead = @(($view + ".{Diagnostics}.Reading.TagHandles")) $authReqBody = "{ 'username':'$userName', 'password':'$password', 'timezone':'Eastern Standard Time', 'application':'$application' }" $userToken = (Invoke-WebRequest -Uri $baseURL"getUserToken" -Method POST -Body $authReqBody | ConvertFrom-Json).userToken $tagJson = $tagsToRead -join "','" $reqBody = "{ 'userToken':'$userToken', 'tags': ['" + $tagJson + "'] }" $tagData = (Invoke-WebRequest -Uri http://ourservice/api/v1/getTagData -Method POST -Body $reqBody).Content $hashtable = @{} (ConvertFrom-Json $tagData).psobject.properties | Foreach { $hashtable[$_.Name] = $_.Value } $hashtableData = @{} foreach ($prop in $hashtable['data'].PSObject.Properties) { $hashtableData[$prop.Name] = $prop.Value[1] } Write-Host @" <prtg> <result> <channel>Handles</channel> <value>24</value> </result> <result> <channel>clients</channel> <unit>clients</unit> <value>4</value> </result> <text>Hello</text> </prtg> "@ $revokeBody = "{ 'userToken':'$userToken' }" $revokeResponse = Invoke-WebRequest -Uri http://ourservice/api/v1/revokeUserToken -Method POST -Body $revokeBody
Output
PS C:\Program Files (x86)\PRTG Network Monitor\Custom Sensors\EXEXML> .\GetDiagnostics.ps1
<prtg>
<result>
<channel>Handles</channel>
<value>24</value>
</result>
<result>
<channel>clients</channel>
<unit>clients</unit>
<value>4</value>
</result>
<text>Hello</text>
</prtg>
Add comment