Question

I am trying to search for a process on a remote machine and pass an exit code so it can be handled by another process. So if the process exists output exit code 1, if not do nothing. I wrote the script below with some help from another post. It works for a localmachine/local process but returns nothing for a remote process/machine. In the script below if i use the standalone tasklist command it works.

@echo off
setlocal enableDelayedExpansion

set "cmd=tasklist.exe /NH /s RemoteMachine /u RemoteMachine\administrator /p Password /fi "Imagename eq Install.exe""

for /F "delims=*" %%p in ('!cmd! ^| findstr "Install.exe" ') do (
  echo exit 1
)
Was it helpful?

Solution

I would advise you to change the logic behind returning exit codes. Windows commands like FINDSTR use a different logic for that: if there's a match, the exit code is 0, and if there's no match, it's 1. And because the logic is already implemented in FINDSTR, you could just use it:

@tasklist.exe /NH /s RemoteMachine /u RemoteMachine\administrator /p Password /fi "Imagename eq Install.exe" | findstr "Install.exe" >nul
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top