Batch file to check a process that running on other PC

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
loc3loan
Posts: 23
Joined: 16 Jan 2014 07:57

Batch file to check a process that running on other PC

#1 Post by loc3loan » 17 Oct 2014 06:35

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.

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

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

#2 Post by Squashman » 17 Oct 2014 06:44

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"

loc3loan
Posts: 23
Joined: 16 Jan 2014 07:57

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

#3 Post by loc3loan » 17 Oct 2014 07:53

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.

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

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

#4 Post by Squashman » 17 Oct 2014 07:59

Your password shows an exclamation point. Do you have delayed expansion disabled?

loc3loan
Posts: 23
Joined: 16 Jan 2014 07:57

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

#5 Post by loc3loan » 17 Oct 2014 08:03

what do you mean about the delayed expansion? Thanks.

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

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

#6 Post by Squashman » 17 Oct 2014 08:14

Post the entire batch file you are using.

loc3loan
Posts: 23
Joined: 16 Jan 2014 07:57

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

#7 Post by loc3loan » 17 Oct 2014 08:17

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.

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

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

#8 Post by Compo » 17 Oct 2014 08:39

…as was aluded to by Squashman your second line is enabling delayed expansion, so change En to Dis.

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

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

#9 Post by Squashman » 17 Oct 2014 08:47

Umm, do you not see this in your code: Setlocal EnableDelayedExpansion :?: :?: :?: :?: :?: :?: :roll:

loc3loan
Posts: 23
Joined: 16 Jan 2014 07:57

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

#10 Post by loc3loan » 17 Oct 2014 09:06

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.

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

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

#11 Post by Compo » 17 Oct 2014 11:34

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.

Yury
Posts: 115
Joined: 28 Dec 2013 07:54

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

#12 Post by Yury » 17 Oct 2014 12:38

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

loc3loan
Posts: 23
Joined: 16 Jan 2014 07:57

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

#13 Post by loc3loan » 20 Oct 2014 07:21

Thank you so much everyone. Learned something new again. Have a good day.

Post Reply