This article applies as of PRTG 22
Correct display of special characters in EXE/Script sensors
If you use an EXE/Script sensor or an EXE/Script Advanced sensor and the executed script returns encoded special characters (for example, diacritics), PRTG might not correctly display these characters in the sensor message or in channel names.
As of PRTG 15.2.16, you can explicitly define the encoding in the target script. In previous versions, PRTG tried to determine the encoding merely by chance. This did not work in every case.
You must provide the following information in the script:
- Your script must start with the following line:
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
- If you use the EXE/Script Advanced sensor that returns XML, you must additionally add the following line at the beginning of the XML output:
<?xml version=`"1.0`" encoding=`"UTF-8`" ?>
Note the grave accents (`) in front of the quotation marks encoding the attributes. These are necessary for correct escaping.
- The text of both the EXE/Script and the EXE/Script Advanced sensor must be returned with the following command at the end of the script:
[Console]::WriteLine($output)
Defining your script this way ensures the correct display of special characters in PRTG.
Note: VBScript does not seem to be able to encode text in UTF-8. Use PowerShell instead.
Note: For a proper sensor overview, change the encoding of the .ps1 file from UTF-8 to UTF-8-BOM.
Note: If you use an SSH Script Advanced sensor, you must also provide a correct XML declaration in the script.
Update PRTG 17.1.29: As of PRTG 17.1.29, you can use escape sequences for special characters (for example, °). Note that this only applies to sensor message and custom units, it does not work with channel names.
Default encoding
If you do not define the encoding in PRTG 15.2.16 or later, PRTG takes the following default encoding:
- EXE/Script sensors: OEMCP encoding from HLKM\SYSTEM\CurrentControlSet\Control\Nls\CodePage
- EXE/Script Advanced sensors: UTF-8 for both XML and JSON
Sample structure of a corresponding PowerShell script:
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
$output = @"
<?xml version=`"1.0`" encoding=`"UTF-8`" ?>
<prtg>
<result>
[…]
</result>
</prtg>
"@
[Console]::WriteLine($output)
Error message The handle is invalid
I get the error message The handle is invalid when I try to run the script.
This happens sometimes for reasons that are yet unknown. To fix the issue, instead of starting the script with the output encoding, use the following command:
# create temporary console
ping localhost -n 1 | Out-Null
# set output encoding
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
Further information about this can be found on Microsoft TechNet Powershell ISE vs [Console]::OutputEncoding.
Add comment