PDA

View Full Version : Any Windows scripting gurus?



somebody
18-06-2012, 10:01 PM
I need a .bat script to run on a Windows Server 2k3 machine to help work around some limitations of a legacy server application.

In particular, it needs to:
- Check if a particular process is running (blah.exe) under a particular account (myapplicationserviceaccount). There should be 4 copies of blah.exe run as myapplicationserviceaccount
- If there are no "blah.exe" processes running as myapplicationserviceaccount, it should run startmyapplication.bat which will start them
- If there are fewer than 4 copies of blah.exe running as myapplicationserviceaccount, it will terminate the remaining blah.exe processes, then run startmyapplication.bat

If this was on a Unix/Linux environment it would be easy, but I don't know my way around Windows scripting very well. Currently I have a script which uses qprocess.exe to detect if there is one or more instance of blah.exe running, but I can't find any way to count the number of blah.exe processes running (noting that blah.exe MUST be run by myapplicationserviceaccount).

Any pointers/ideas/suggestions?

No I can't use PowerShell, and no I can't install Cygwin.

fred_fish
18-06-2012, 10:09 PM
for /f "tokens=*" %%a in ('<qprocess_test>^| find -c "processname"') do set processcount=%%a
if not %processcount%==4 then do stuff
maybe helps?

somebody
18-06-2012, 10:20 PM
Brilliant! Thanks fred_fish - just what I was after.

fred_fish
18-06-2012, 11:01 PM
"tokens=*" may be superfluous, or may need to = the count field.
I can't recall the output format of find -c as I replaced it with a win32 grep a long time ago :)

somebody
19-06-2012, 08:31 AM
Thanks - "find /C" returns a count - there doesn't seem to be a "find -c"

Any idea how I'd kill off unwanted blah.exe processes?

fred_fish
19-06-2012, 10:09 AM
Thanks - "find /C" returns a count - there doesn't seem to be a "find -c"
Oh, yeah ... more mental cobwebs (grep -c is count)

Any idea how I'd kill off unwanted blah.exe processes?

pskill (http://technet.microsoft.com/en-us/sysinternals/bb896683.aspx)

somebody
19-06-2012, 08:36 PM
Perfect - thanks for your help.