External Powershell Scripts return code
Posted: Mon Feb 13, 2017 11:21 am
I'm having difficulty getting an externally run Powershell script to return the correct exit code.
The script evaluates a URL (which is passed in as an argument) and considers a status description of "IP address not allowed" as a success, exit code 0, a status description of "OK" as Critical, and anything else as a warning.
The script is:
If I run this locally and pass in a URL as an argument I get:
And I can see that the $LASTEXITCODE has the correct value of 2
If I run using nscp test I get: So it looks like it should return the correct exit code - however, if I try to run from my Nagios box I get:
[[email protected] ~]$ /usr/local/nagios/libexec/check_nrpe -H 10.12.0.4 -t 30 -n -c check_ip_filter -a google.com
Warning, Unknown response |
And in my nsclient.log I can see the correct command:
2017-02-13 15:22:44: debug:c:\source\master\modules\CheckExternalScripts\CheckExternalScripts.cpp:607: Command line: cmd /c echo scripts\powershell\IPFiltercheck.ps1 google.com; exit $LastExitCode | powershell.exe /noprofile -command -
When I run that command line it returns exit code 2, so I'm not sure why the Nagios box is getting back the unknown response which is the exit 1?
The script evaluates a URL (which is passed in as an argument) and considers a status description of "IP address not allowed" as a success, exit code 0, a status description of "OK" as Critical, and anything else as a warning.
The script is:
Code: Select all
param ([string]$url)
try
{
$response = Invoke-WebRequest -Uri $url
}
catch
{
$response = $_.Exception.Response
}
if ($response.statusDescription -eq "IP address not allowed")
{
Write-Output "OK, IP Filtering is working |"; exit 0
}
if ($response.StatusDescription -eq "OK")
{
Write-Output "Critical, IP Filtering is not in place for $url |"; exit 2
}
else { write-Output "Warning, Unknown response |"; exit 1 }Code: Select all
PS C:\Program Files\NSClient++\scripts\PowerShell> .\IPFiltercheck.ps1 google.com
Critical, IP Filtering is not in place for google.com If I run using nscp test I get: So it looks like it should return the correct exit code - however, if I try to run from my Nagios box I get:
[[email protected] ~]$ /usr/local/nagios/libexec/check_nrpe -H 10.12.0.4 -t 30 -n -c check_ip_filter -a google.com
Warning, Unknown response |
And in my nsclient.log I can see the correct command:
2017-02-13 15:22:44: debug:c:\source\master\modules\CheckExternalScripts\CheckExternalScripts.cpp:607: Command line: cmd /c echo scripts\powershell\IPFiltercheck.ps1 google.com; exit $LastExitCode | powershell.exe /noprofile -command -
When I run that command line it returns exit code 2, so I'm not sure why the Nagios box is getting back the unknown response which is the exit 1?