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
211 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

More or less everybody in the IT market understands the basics of Networking, however when we start talking about Storage Networks, things get a bit dizzier, and this is where I would like to help.
Hyper-convergence systems have taken the IT world by storm and have quickly started to change our point of view of how the data center should and could be architected. In this article, I’ll explain the benefits of employing a hyper-converged system …
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
The viewer will learn how to dynamically set the form action using jQuery.

867 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

16 Experts available now in Live!

Get 1:1 Help Now