Page 1 of 1

External Powershell Scripts return code

Posted: Mon Feb 13, 2017 11:21 am
by hales8181
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:

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 }
If I run this locally and pass in a URL as an argument I get:

Code: Select all

PS C:\Program Files\NSClient++\scripts\PowerShell> .\IPFiltercheck.ps1 google.com
Critical, IP Filtering is not in place for google.com 
And I can see that the $LASTEXITCODE has the correct value of 2

If I run using nscp test I get:
nscptest.png
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?

Re: External Powershell Scripts return code

Posted: Mon Feb 13, 2017 11:45 am
by askewdread
not sure if it does anything differently but we use the following for our exit part in the config..... its slightly different than yours so may make a difference?

Code: Select all

exit($lastexitcode)

Re: External Powershell Scripts return code

Posted: Mon Feb 13, 2017 2:02 pm
by rkennedy
Does the account running NSClient++ have all the correct permissions for executing your script? Might be worth having it debug a temp file to check if it's actually being executed at all, to debug it further.

Re: External Powershell Scripts return code

Posted: Tue Feb 14, 2017 7:45 am
by hales8181
exit($lastexitcode) fixed it! Thank you!

Re: External Powershell Scripts return code

Posted: Tue Feb 14, 2017 5:47 pm
by dwhitfield
It sounds like this issue has been resolved. Is it okay if we lock this thread? Thanks for choosing the Nagios forums!

Re: External Powershell Scripts return code

Posted: Mon Feb 20, 2017 6:38 am
by hales8181
Turned out I needed to do both - exit code and change the nsclient service to run as admin.

Thread can be locked