Link to home
Start Free TrialLog in
Avatar of Halpinpartners
Halpinpartners

asked on

How could I write a script or batch file that will test if a particular application is running on a remote server and produce a message on who is using that particular application.

We are running a SBS Window 2003 premium SP2 environment with a file server (Win2K3)R2 SP2 and several (win2K3) R2 SP2 Terminal Servers, on our LAN / WAN.
As part of an assignment I am completing (by using a real work example) I need to create a script that a remote user can use to run when there is no IT support available after hours.  The remote user does not have administrative rights.
The script has to restart a service; I intend to use the PSService tool from Sysinternals / Microsoft "psservice \\remote_computer u Username p Password restart Service_name
Before this command executes, I need the batch file or script to check if any other user/s have a particular application (dependent on the service) open and for it to return a message to tell me if there is, e.g. %Username% is running Program_Name. Advise them to exit the program before continuing. Press C when you are ready to Continue or X to Exit Id have the batch file re-check or exit depending on the users selection. If the initial or subsequent test showed no-one had that particular application running, the batch file would then execute the PSService command line.

I have an introductory knowledge of DOS commands and batch files, but have no experience in WMI scripting.
Avatar of dan_blagut
dan_blagut
Flag of France image

Hi

You have a command name tasklist, that list all task on that computer. You can redirect-it in a temporary text file, then analise that text file in a vbs script.

Dan
following line should do what you want:
for /f "skip=1 tokens=7 delims=," %i in ('tasklist /v /FI "IMAGENAME eq YOURAPPLICATION.exe" /fo csv')do @echo User %i is using YOURAPPLICATION.exe


ofcourse when use it in *.cmd script you should use double%
for /f "skip=1 tokens=7 delims=," %%i in ('tasklist /v /FI "IMAGENAME eq YOURAPPLICATION.exe" /fo csv')do @echo User %%i is using YOURAPPLICATION.exe
for /f "skip=1 tokens=7 delims=," %i in ('tasklist /v /FI "IMAGENAME eq YOURAPPLICATION.exe" /fo csv')do @echo User %i is using YOURAPPLICATION.exe

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of thefaza
thefaza
Flag of Poland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Halpinpartners
Halpinpartners

ASKER

Hi thefaza,

I think this is going to work.  Currently testing it. but i have noticed that the tokens number is differnet when tesing a local or remote terminal server.  so that i understand what is happening are you able to point me towards a site that explains the various commands and variables and switches.  I would really appriciate learning more on this subject.

Cheers
Option token says which column in output rows is assigned for variable %i in my case local system was WINDOWS 2008 and remote was windows 2003, tasklist gives bit different output on both systems, in W2k8 there is additional column "Status" so column "user" has number 7 on W2k3 there is no column "status" so "user" has number 6. You just have to issue command tasklist /v /fo csv and count which column contain Username. The option skip=1 skips first line which contains column names
Sorry for the delay in responding, i've been out of town for a Branch relocation. Thank you SO much for your assistance & links to scripting sites. I am learning lots from them. Regards Shaun.