Hi. This may sound like a repeated question, but as far as I can tell, I've tried all the suggestions I've found, and I'm still having an issue.
I have a powershell script that, no matter what exit code I return, the sensor shows a warning state immediately after running, then shows an error state. Regardless, I still get the output I want, but I'd like to know what I'm doing wrong in regards to the exit state.
I have the settings set to set placeholders as environment variables, and I pass %device as a parameter.
I also selected "Use Windows credentials of parent device."
When I run the code from the command line or from an ISE, it works fine. No errors.
My code is as follows:
$BeginDate = $null $LogResults = $null $BeginDate=[System.Management.ManagementDateTimeConverter]::ToDMTFDateTime((get-date).AddMinutes(-1)) Try { $LogResults = get-wmiobject -class win32_ntlogevent -ComputerName $args[0] -filter "(Type='Error') and (LogFile='Application') and (TimeGenerated >'$BeginDate') and (SourceName <> 'GetEnginefiles')" If ($LogResults) { $FormattedResult = New-Object System.Text.StringBuilder foreach ($Result in $LogResults) { [void]$FormattedResult.AppendLine($Result.LogFile.ToString()) [void]$FormattedResult.AppendLine($Result.Type.ToString()) [void]$FormattedResult.AppendLine($Result.Message.ToString()) [void]$FormattedResult.AppendLine("") } Write-Host "1:$FormattedResult" exit 1 } else { Write-Host "0:OK" exit 0 } } Catch { Write-Host "2:Error" exit 2 }
Article Comments
Right now, it says "#01320:", then gives me the output I'm looking for.
I'm not finding a results file, even after changing the result handling.
Mar, 2013 - Permalink
The precise path to the output file should by standard be the following
C:\ProgramData\Paessler\PRTG Network Monitor\Logs (Sensors)
Please note that this is a hidden folder
What precise sensor type are you using? The EXE/Script or the EXE/Script Advanced Sensor?
Mar, 2013 - Permalink
It appears that PRTG does not like the long variable I returned as the $FormattedResult. I returned a much shorter, simpler message instead, and it seems to work. I suppose I could write code in the sensor itself that sends me the long output in an email, but just returns a short, generic message to the sensor.
Mar, 2013 - Permalink
You are correct, the length of the MESSAGE variable is automatically cut at 255 characters. So it is not possible to return longer messages to PRTG. Sorry. What message is the sensor showing?
Mar, 2013 - Permalink
Hi,
what is the precise error message you are receiving? What is the output of the script when run manually?
May you additional activate the debug mode of the sensor? That can be done on tab Settings of the same. There set option Result Handling to Write Result to disk. This will create a file called Result of Sensor XXXX.txt in the subfolder Logs (Sensors) of your PRTG Data Directory.
What is the file content?
Mar, 2013 - Permalink