Solved

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.

Posted on 2008-06-24
8
210 Views
Last Modified: 2010-04-21
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.
0
Comment
Question by:Halpinpartners
  • 5
  • 2
8 Comments
 
LVL 21

Expert Comment

by:dan_blagut
ID: 21863553
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
0
 
LVL 2

Expert Comment

by:thefaza
ID: 21864210
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

0
 
LVL 2

Accepted Solution

by:
thefaza earned 500 total points
ID: 21864273
I forgot, for remote server you need use tasklist command with switches /s /u /p:
for /f "skip=1 tokens=7 delims=," %i in ('tasklist /s REMOTE_SERVER /u USERNAME /p PASSWORD /v /FI "IMAGENAME eq YOURAPPLICATION.exe" /fo csv')do @echo User %i is using YOURAPPLICATION.exe

(depend on the version OS there can be another number of token, ex on windows server 2008 it is 7 but on windows 2003 it is probably 6)
0
 

Author Comment

by:Halpinpartners
ID: 21872247
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
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 2

Expert Comment

by:thefaza
ID: 21872395
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
0
 
LVL 2

Expert Comment

by:thefaza
ID: 21872424
0
 
LVL 2

Expert Comment

by:thefaza
ID: 21873886
0
 

Author Closing Comment

by:Halpinpartners
ID: 31470422
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.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
A quick step-by-step overview of installing and configuring Carbonite Server Backup.
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now