Search code examples
powershellnagios

Close Powershell Child Session of my batch file wrapper


I have a problem to use a powershell script plugin, with my nagios agent (NRPE_NT.exe).

To solve this problem I've create underlying wrap batch file:

@ECHO OFF

SET SCRIPTPATH=%~d0%~p0check_process_mem.ps1

SET ARGS=%*
IF [%ARGS%] NEQ [] GOTO ESCAPE_ARGS

:POWERSHELL
PowerShell.exe -NoProfile -NonInteractive -NoLogo -ExecutionPolicy Unrestricted -Command "& { $ErrorActionPreference = 'Stop'; & '%SCRIPTPATH%' @args; EXIT $LASTEXITCODE }" %ARGS%
EXIT /B %ERRORLEVEL%

:ESCAPE_ARGS
SET ARGS=%ARGS:"=\"%
SET ARGS=%ARGS:`=``%
SET ARGS=%ARGS:'=`'%
SET ARGS=%ARGS:$=`$%
SET ARGS=%ARGS:{=`{%
SET ARGS=%ARGS:}=`}%
SET ARGS=%ARGS:(=`(%
SET ARGS=%ARGS:)=`)%
SET ARGS=%ARGS:,=`,%
SET ARGS=%ARGS:^%=%

GOTO POWERSHELL

Now, if I run it as dialog user, I haven't any problem, but if I run it with SYSTEM user, powershell session doesn't close, and I have to kill it, from tasklist.

What can I do, for solve this problem?

I can't update NRPE_NT.exe agent to NSClient+ (powershell compliance).


Solution

  • i've solve this problem with powershell plugin, without wrap batch file. I've declaire nrpe.cfg in this way:

    command[check_X]=cmd /c echo C:\nrpe\libexec\check_process_mem.ps1 $ARG1$ -VM_w "$ARG2$" -VM_c "$ARG3$" ; exit $($LastExitCode)  | powershell.exe -NoProfile -NonInteractive -NoLogo -ExecutionPolicy Unrestricted -command -
    

    and now it works correctly. Exit code and output of script is correct.