We had a need to enumerate all processes that are running in a specific MS terminal server user environment. Borrowed some parts from Rad's EnumProcess UDF and added some extra stuff. Results are below. Comments/suggestions are welcome.

Session ID's are pulled from an export that is automatically generated every x minutes with the "query user >d:\ts_users\tsusers.txt" command from a batch script.

 Code:
Function GetMSTSUserProcesses($TSName, $SessionID)
	Dim $winmgmts, $ExecQuery, $Processes, $Process, $Index

	$winmgmts = "winmgmts:{impersonationLevel=impersonate}!//" + $TSName
	$ExecQuery = "select * from Win32_Process Where SessionID=" + $SessionID
	$Processes = GetObject($winmgmts).ExecQuery($ExecQuery)

	For Each $process in $processes
		$Index = $Index + 1
	Next

	Dim $temparray[$Index]

	For $Index = 0 to UBound($Processes)
		$temparray[$Index] = $Processes[$Index]
	Next

	$Index = 0
	For Each $Process in $Processes
		$temparray[$Index] = $Process.name
		$Index = $Index + 1
	Next

	$GetMSTSUserProcesses = $temparray
EndFunction


Example:
 Code:
$sessionid = "21"
$tsname = "MSTermServer01"

$processes = GetMSTSUserProcesses($tsname, $sessionid)
If UBound($processes) > 0
	? "The user with session ID: " + $sessionid " is working on: " + $tsname + "."
	? "This user has started " + UBound($processes)  + " processes."
	? "Processes started by this users are: "
	For Each $process in $processes
		? $process
	Next
Else
	? "There is no user with session ID: " + $sessionid + " on " + $tsname + "."
EndIf

Sleep 5


Edited by Mart (2009-04-16 09:56 AM)
Edit Reason: Updated code.
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.