WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Terminal Server

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Terminal Server Activity and Performance Monitor/Logger

Keywords: 	  Terminal Server Activity and Performance Monitor/Logger

;******************************************************************************
;*                                                                            *
;*     Terminal Server                                                        *
;*                                                                            *
;*     Activity and Performance Monitor/Logger                                *
;*                                                                            *
;*     Eric Speicher 05/28/2001                                               *
;*                                                                            *
;******************************************************************************

title = "TS Monitor"


;************ list processes **************
#DefineFunction evsListProcTS()
   ;for listing/monitoring processes on Terminal Server
   ;returns format |||@TAB...
   ;AddExtender("wproc34i.dll") ;for process list
    AddExtender("wwprc44i.dll") ;for process list
   proc_list = tListProc()  ;format: [short-processname|processid]@TAB...]
   total_procs = ItemCount(proc_list, @TAB)
   proc_string = ""
   for  xx = 1 to total_procs
      proc_name_ID = ItemExtract(xx, proc_list, @TAB)
      proc_ID = ItemExtract(2, proc_name_ID, "|")
      proc_user = evsProcUserID(proc_ID)
      proc_name_short = ItemExtract(1, ItemExtract(1, proc_name_ID, "|"), ".")
      num_duplicates = evsStrDuplCount(proc_list, proc_name_short, 1)
      if num_duplicates == 1
         proc_name_long = proc_name_short
      else
         proc_name_long = evsProcMatch(proc_ID, proc_name_short, num_duplicates)
      endif
      CPU_query = StrCat("\Process(", proc_name_long, ")\%% Processor Time")
      proc_CPU = 0
      ErrorMode(@OFF)
      proc_CPU = tGetData(CPU_query, 0)
      ErrorMode(@Cancel)
      proc_string = StrCat(proc_string, proc_name_long, "|", proc_ID, "|", proc_user, "|", proc_CPU, @TAB)
   next
   return proc_string
#EndFunction


#DefineFunction evsProcUserId(proc_ID)
   ; *******************************
   ; ** BOOL ProcessIdToSessionId(
   ; ** DWORD dwProcessId,
   ; ** DWORD *pSessionId
   ; *******************************
   dll_file = "M:\WTSRV\system32\kernel32.dll"
   dll_handle = DllLoad(dll_file)
   funct = "ProcessIdToSessionId"
   ppBuffer=BinaryAlloc(4)             ;Allocate buffer for return pointer
   BinaryEODSet(ppBuffer,4)            ;tell WinBatch all data is valid
   session_ID = 0
   if DllCall(dll_handle, long:funct, long:proc_ID, lpbinary:ppBuffer)
      session_ID = BinaryPeek4(ppBuffer,0)
   endif
   DllFree(dll_handle)
   BinaryFree(ppBuffer)
   return session_ID
#EndFunction

#DefineFunction evsStrDuplCount(search_str, sub_str, start)
   ;returns number of times  is found in 
   position = StrIndex(search_str, sub_str, start, @FWDSCAN)
   if !position
      return 0 ; not found this time
   else
      return (1 + evsStrDuplCount(search_str, sub_str, (position + 1)))
   endif
#EndFunction

#DefineFunction evsProcMatch(proc_ID, proc_name_short, num_duplicates)
   ;returns long name e.g. MYPROCESS#3
   AddExtender("wproc34i.dll") ;for process tGetData
         for yy = 1 to num_duplicates
            query_ID = 0
            if yy == 1
               query_proc_name = proc_name_short  
            else
               query_proc_name = StrCat(proc_name_short, "#", (yy - 1))
            endif
            counter_str = StrCat("\Process(", query_proc_name, ")\ID Process")
            query_ID = 0
            ErrorMode(@OFF)
            query_ID = tGetData(counter_str, 0)
            ErrorMode(@Cancel)
            if query_ID == proc_ID
               return query_proc_name
               break
            endif
         next
      return 0
#EndFunction


answer = evsListProcTS()
ClipPut(answer)
Message(title, answer)

Article ID:   W14896
File Created: 2013:04:01:09:19:36
Last Updated: 2013:04:01:09:19:36