I'm trying to run the following Powershell script automatically in PRTG.

$password = Get-Content C:\securestring.txt | convertto-securestring
$username = 
$credentials = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $password
$session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://prod-exch1.prod.local/PowerShell/ -Credential $credentials

Import-PSSession $session -DisableNameChecking

Write-Host "<prtg>"
foreach ($mbx in Get-Mailbox) 
{
    #$name = (Get-MailboxStatistics $mbx.Identity).DisplayName
    $name = $mbx.PrimarySMTPAddress
    $used = (Get-MailboxStatistics $mbx.Identity).TotalItemSize
    Write-Host "<result>" "<channel>$name</channel>" "<value>$used</value>" "<unit>Custom</unit>" "<CustomUnit>MB</CustomUnit>" "<VolumeSize>MegaByte</VolumeSize>" "<float>1</float>" "</result>"
}
Write-Host "</prtg>"
Write-Host 0:OK
Exit 0

The timeout being set to 300 seconds and the scanning interval to 4 hours. It times out with the error code PE018 every time.

The script runs as expected using the Powershell console


Article Comments

Hi,
may you activate the the debug option of the sensor and post the result?
Therefore, on tab Settings of the sensor please set option Result Handling to Write Result to Disk. Then scan the sensor once manually via Scan Now.
This will create a file called Result of Sensor XXXX.txt in the Logs (Sensors) subdirectory of your PRTG Data Directory.
Best regards


Apr, 2013 - Permalink

Yes, I have just done so, and I'm sorry for the late answer. Was swarmed yesterday ;-)

Here is the result:

ModuleType Name ExportedCommands ---------- ---- ---------------- Script tmp_c60f8aec-971c-4a7e... {Get-IRMConfiguration, New-MailUser, En... <prtg> <result> <channel>uu-lillebaelt.dk</channel> <value>17761284946</value> <unit>Custom</unit> <CustomUnit>MB</CustomUnit> <VolumeSize>MegaByte</VolumeSize> <float>1</float> </result> </prtg> 0:200:Everything OK


Apr, 2013 - Permalink

As an edition to my previous reply, the problem has changed a bit, it is now returning with error code PE132, the format is not well formed.

I've come to the conclusion that it is this part of the response that is giving me trouble:

ModuleType Name ExportedCommands ---------- ---- ---------------- Script tmp_c60f8aec-971c-4a7e... {Get-IRMConfiguration, New-MailUser, En...

But sadly I have no idea how to remove that bit in a Powershell script.

Furthermore, is it necessary to output 0:200:Everything OK?


Apr, 2013 - Permalink

To suppress the first three lines, the output of the import-PSSession command has to be piped to null. Please adjust the according line to:

Import-PSSession $Session -DisableNameChecking | out-null

Additional, you are returning two types of return values.

<prtg>
    <result>
        <channel>uu-lillebaelt.dk</channel>
        <value>17761284946</value>
        <unit>Custom</unit>
        <CustomUnit>MB</CustomUnit>
        <VolumeSize>MegaByte</VolumeSize>
        <float>1</float>
    </result>
</prtg>

This would be the output needed for the EXE/Script Advanced Sensor but this output

0:200:Everything OK

Would be for the EXE/Script Sensor. So depending on the sensor type you are using you will have to adjust the output of the script. You cannot return both types of output.
When you have adjusted your script the values should be picked up by PRTG correctly.


Apr, 2013 - Permalink

Everything works now, thank you very much for you assistance :-)


Apr, 2013 - Permalink