WinBatch® Technical Support Forum

All Things WinBatch => WinBatch => Topic started by: Jeremy Whilde on February 25, 2014, 10:33:20 AM

Title: Number of process instances on TS
Post by: Jeremy Whilde on February 25, 2014, 10:33:20 AM
I am using the following at the top of a MyApp.exe WB compiled script to check the number of copies of the MyApp.exe that are running, which works.
But I now need to run on a Windows terminal server and have a number of clients/users logon and each run an instance of the exe.

;---------------------------------------------------------------------
; Check if already running ***Process name inc .exe***
;---------------------------------------------------------------------
#DefineFunction udfAppExist(processname)
   objWMIService = GetObject("WinMgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
   colItems = objWMIService.ExecQuery("Select * from Win32_Process where Caption = '%processname%'")
   count = colItems.count
   retvalue = 0
   if count > 0 then retvalue = count             ; Set return value to number of processes found
   colItems = ""
   objWMIService = ""
   Return retvalue
#EndFunction

CheckRun = udfAppExist("MyApp.exe")     ;***Remember to include .exe in the process name***
If CheckRun == 2 then Exit         ; Needs to be 2 so if second process runs it will exit allow the 1 original to run
; Name application
WinTitle ("","MyApp")
; Rest of the exe script here

I am guessing if I can determine the owner of each of the processes that I am counting I could then
qualify the CheckRun for each user that logs onto the  terminal server and tries to run the MyApp exe and limit this to 1, but not sure how to do this? 
or is there a better way?

Has anybody done something similar to this for running on WTS?

Thanks JW

Title: Re: Number of process instances on TS
Post by: ChuckC on February 25, 2014, 10:40:15 AM
The WMI class "Win32_Process " has a property named "SessionId" which should be the RDP/TS Session # that any particular process is associated with.  As you examine each instance of Win32_Process that is returned by your query, you will need to examine the value of this property and keep track of how many processes are associated with each unique session id value.
Title: Re: Number of process instances on TS
Post by: Deana on February 25, 2014, 11:48:19 AM
Check out the Terminal Service Extender function wtsEnumProcesses.