Search code examples
batch-filefor-loopremote-servertasklist

Test if process is running on remote units and output to log


We would like to check if a certain process is running on any of our listed servers, outputting the result to a log file like this:

SERVERNAME Process is running
SERVERNAME Process is not running

I'm new to batch but this is how far I got:

FOR /F "TOKENS=*" %%A IN (LIST.TXT) DO TASKLIST /S %%A /FI "IMAGENAME EQ IEXPLORE.EXE" >> ECHO %%A D:\SEARCH.LOG  

Solution

  • This should pretty much do what you want.

      @echo off
       setlocal enabledelayedexpansion
       for /F "delims=" %%a in (list.txt) do (
         tasklist /s %%a | find /I "iexplore.exe" >nul
          if !errorlevel! equ 0 (echo %%a Process is Running) else (echo %%a Process is Not Running)
     ) >> d:\search.log
    

    As for your requirement on RPC errors, perhaps consider rpcping and echo to file if not available.

    @echo off
    setlocal enabledelayedexpansion
    for /F "delims=" %%a in (list.txt) do (
        rpcping -s %%a |find /i "Completed" >nul
        if not !errorlevel! equ 0 echo %%a RPC Server not available
        tasklist /s %%a | find /I "iexplore.exe" >nul
        if !errorlevel! equ 0 (echo %%a Process is Running) else (echo %%a Process is Not Running)
     ) >> d:\search.log