Page 1 of 1

Batch file to check a process that running on other PC

Posted: 17 Oct 2014 06:35
by loc3loan
Hello all,

Is there a way that a batch file can check a process that is running on other PC on the same network? For instance, if I need to check to see notepad.exe is running on mypc1, before it runs notepad.exe on mypc2. Both mypc1 and mypc2 are on the same network. Thanks for your help.

Re: Batch file to check a process that running on other PC

Posted: 17 Oct 2014 06:44
by Squashman

Code: Select all

C:\tasklist /?


Code: Select all

TASKLIST [/S system [/U username [/P [password]]]]
         [/M [module] | /SVC | /V] [/FI filter] [/FO format] [/NH]

Description:
    This tool displays a list of currently running processes on
    either a local or remote machine.

Parameter List:
   /S     system           Specifies the remote system to connect to.

   /U     [domain\]user    Specifies the user context under which
                           the command should execute.

   /P     [password]       Specifies the password for the given
                           user context. Prompts for input if omitted.

   /M     [module]         Lists all tasks currently using the given
                           exe/dll name. If the module name is not
                           specified all loaded modules are displayed.

   /SVC                    Displays services hosted in each process.

   /V                      Displays verbose task information.

   /FI    filter           Displays a set of tasks that match a
                           given criteria specified by the filter.

   /FO    format           Specifies the output format.
                           Valid values: "TABLE", "LIST", "CSV".

   /NH                     Specifies that the "Column Header" should
                           not be displayed in the output.
                           Valid only for "TABLE" and "CSV" formats.

   /?                      Displays this help message.

Filters:
    Filter Name     Valid Operators           Valid Value(s)
    -----------     ---------------           --------------------------
    STATUS          eq, ne                    RUNNING |
                                              NOT RESPONDING | UNKNOWN
    IMAGENAME       eq, ne                    Image name
    PID             eq, ne, gt, lt, ge, le    PID value
    SESSION         eq, ne, gt, lt, ge, le    Session number
    SESSIONNAME     eq, ne                    Session name
    CPUTIME         eq, ne, gt, lt, ge, le    CPU time in the format
                                              of hh:mm:ss.
                                              hh - hours,
                                              mm - minutes, ss - seconds
    MEMUSAGE        eq, ne, gt, lt, ge, le    Memory usage in KB
    USERNAME        eq, ne                    User name in [domain\]user
                                              format
    SERVICES        eq, ne                    Service name
    WINDOWTITLE     eq, ne                    Window title
    MODULES         eq, ne                    DLL name

NOTE: "WINDOWTITLE" and "STATUS" filters are not supported when querying
      a remote machine.

Examples:
    TASKLIST
    TASKLIST /M
    TASKLIST /V /FO CSV
    TASKLIST /SVC /FO LIST
    TASKLIST /M wbem*
    TASKLIST /S system /FO LIST
    TASKLIST /S system /U domain\username /FO CSV /NH
    TASKLIST /S system /U username /P password /FO TABLE /NH
    TASKLIST /FI "USERNAME ne NT AUTHORITY\SYSTEM" /FI "STATUS eq running"

Re: Batch file to check a process that running on other PC

Posted: 17 Oct 2014 07:53
by loc3loan
I can run this command in command promt:

tasklist /S dom-dnfconfig /U dom-dnfconfig\systemadm /P P$5$C$! /fi "IMAGENAME eq notepad.exe"

But, when I put it in the batch file, it gave me an error with unknown username or bad password.

Could you please let me know what is wrong with it? Thanks.

Re: Batch file to check a process that running on other PC

Posted: 17 Oct 2014 07:59
by Squashman
Your password shows an exclamation point. Do you have delayed expansion disabled?

Re: Batch file to check a process that running on other PC

Posted: 17 Oct 2014 08:03
by loc3loan
what do you mean about the delayed expansion? Thanks.

Re: Batch file to check a process that running on other PC

Posted: 17 Oct 2014 08:14
by Squashman
Post the entire batch file you are using.

Re: Batch file to check a process that running on other PC

Posted: 17 Oct 2014 08:17
by loc3loan

Code: Select all

@echo off

Setlocal EnableDelayedExpansion

Set _password="P$5$C$!3"

tasklist /S dom-dnfconfig /U dom-dnfconfig\nagiosadm /P %_password% /fi "IMAGENAME eq notepad.exe" >NUL

if ERRORLEVEL 1 echo "program is running"

if ERRORLEVEL 2 start notepad.exe

PAUSE


MOD EDIT: PLEASE USE CODE TAGS!!!!!!!

Thanks.

Re: Batch file to check a process that running on other PC

Posted: 17 Oct 2014 08:39
by Compo
…as was aluded to by Squashman your second line is enabling delayed expansion, so change En to Dis.

Re: Batch file to check a process that running on other PC

Posted: 17 Oct 2014 08:47
by Squashman
Umm, do you not see this in your code: Setlocal EnableDelayedExpansion :?: :?: :?: :?: :?: :?: :roll:

Re: Batch file to check a process that running on other PC

Posted: 17 Oct 2014 09:06
by loc3loan
I thought Setlocal EnableDelayedExpansion, to enable the function and it allows you pass the information to it. I am still learning the Batch file. Thanks for your help.

Re: Batch file to check a process that running on other PC

Posted: 17 Oct 2014 11:34
by Compo
Now that you've got that sorted, the top of your script could look something like this:

Code: Select all

@Echo Off
Setlocal DisableDelayedExpansion
Set "_computer=dom-dnfconfig"
Set "_domainuser=%_computer%\nagiosadm"
Set "_password=P$5$C$!3"
Set "_exe=notepad.exe"
Set "_filtername=ImageName eq %_exe%"

TaskList /s %_computer% /u %_domainuser% /p %_password% /fi "%_filtername%">Nul

The bottom however needs changing, apart from the fact that failure is usually detected by a non zero errorlevel tasklist does not output an errorlevel you can use.

Additionally, you will not be able to start notepad on the remote computer like that either.

Re: Batch file to check a process that running on other PC

Posted: 17 Oct 2014 12:38
by Yury

Code: Select all

@echo off


setlocal enabledelayedexpansion


set _password=P$5$C$^^^^^^!3
set _password
echo "%_password%"
echo.

set "_password=P$5$C$^^^!3"
set _password
echo "%_password%"
echo.

set _password="P$5$C$^^^!3"
set _password
echo %_password%
echo.

set _password=P$5$C$^^!3
set _password
echo "!_password%!"
echo.

set "_password=P$5$C$^!3"
set _password
echo "!_password!"
echo.

set _password="P$5$C$^!3"
set _password
echo !_password%!
echo.


endlocal
pause>nul

Re: Batch file to check a process that running on other PC

Posted: 20 Oct 2014 07:21
by loc3loan
Thank you so much everyone. Learned something new again. Have a good day.