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