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
213 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
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 

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
 
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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Moving your enterprise fax infrastructure from in-house fax machines and servers to the cloud makes sense — from both an efficiency and productivity standpoint. But does migrating to a cloud fax solution mean you will no longer be able to send or re…
Restoring deleted objects in Active Directory has been a standard feature in Active Directory for many years, yet some admins may not know what is available.
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…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

839 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