Windows Powershell scripts are returning wrong error codes

Powershell scripts running on Windows Server 2019 are not returning the right error code in the Sensu UI. The powershell exit code is exit 2, but the sensu UI is seeing it as a warning, exit code 1. I am running Sensu-go binary 6.1.3 on the backends as well as the agents. It is all powershell scripts doing the same thing. There is nothing in logs to say why this is happening, I have tried to change different error codes, and it always returns with either 1 or 0. I cannot get it to return a critical.

Any advice on how I can troubleshoot/fix this?

Thanks.

Function to help the exitcode be seen by Sensu

function ExitWithCode
{
param
(
$exitcode
)

$host.SetShouldExit($exitcode)
exit

}

If ($Value -ge $CRITICAL) { Write-Host CheckWindowsCpuLoad CRITICAL: CPU at $Value%. ExitWithCode 2 } If ($Value -ge $WARNING) { Write-Host CheckWindowsCpuLoad WARNING: CPU at $Value%. ExitWithCode 1 } Else { Write-Host CheckWindowsCpuLoad OK: CPU at $Value%. ExitWithCode 0 }

Thanks a lot. That helped!