Go Premium for a chance to win a PS4. Enter to Win

x
?
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
Medium Priority
?
219 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 22

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 2000 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
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 

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

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

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

ADCs have gained traction within the last decade, largely due to increased demand for legacy load balancing appliances to handle more advanced application delivery requirements and improve application performance.
In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Suggested Courses

971 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