Page 1 of 1 1
Topic Options
#193443 - 2009-04-15 04:40 PM RFC on GetMSTSUserProcesses() UDF
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
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.

Top
#193446 - 2009-04-15 09:56 PM Re: RFC on GetMSTSUserProcesses() UDF [Re: Mart]
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11629
Loc: CA
Why a batch file? Not that in some cases it's a bad thing but I'm sure someone is going to come along and want it in pure KiX code.

And is that ALL processes or only those of a logged on or remote Admin account, or is that the point? You only want to pull what is running for a specific user?

Top
#193448 - 2009-04-16 01:17 AM Re: RFC on GetMSTSUserProcesses() UDF [Re: NTDOC]
Gargoyle Offline
MM club member
*****

Registered: 2004-03-09
Posts: 1597
Loc: Valley of the Sun (Arizona, US...
You could always use the QWINSTA with UserID to get the session ID as well

 Quote:

K:\>qwinsta /?
Display information about Terminal Sessions.

QUERY SESSION [sessionname | username | sessionid]
[/SERVER:servername] [/MODE] [/FLOW] [/CONNECT] [/COUNTER]

sessionname Identifies the session named sessionname.
username Identifies the session with user username.
sessionid Identifies the session with ID sessionid.
/SERVER:servername The server to be queried (default is current).
/MODE Display current line settings.
/FLOW Display current flow control settings.
/CONNECT Display current connect settings.
/COUNTER Display current Terminal Services counters information.
_________________________
Today is the tomorrow you worried about yesterday.

Top
#193453 - 2009-04-16 09:08 AM Re: RFC on GetMSTSUserProcesses() UDF [Re: NTDOC]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
 Originally Posted By: NTDOC
Why a batch file? Not that in some cases it's a bad thing but I'm sure someone is going to come along and want it in pure KiX code.

And is that ALL processes or only those of a logged on or remote Admin account, or is that the point? You only want to pull what is running for a specific user?


Why a batch file? Dunno. Think I'm gonna change it to pure kix soon.

It gets the processes from one specific user. So if a user has SessionID 12 you should set the $SessionID to 12.
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#193454 - 2009-04-16 09:10 AM Re: RFC on GetMSTSUserProcesses() UDF [Re: Gargoyle]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
Nice one Garg. Will do some reading on it and if required I'll do some tests.
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#193455 - 2009-04-16 09:15 AM Re: RFC on GetMSTSUserProcesses() UDF [Re: Mart]
Arend_ Moderator Offline
MM club member
*****

Registered: 2005-01-17
Posts: 1896
Loc: Hilversum, The Netherlands
Nice one Mart, I just think you made a typo here:
 Code:
$GetMSTSUserProcesses[$Undex] = $Processes[$Index]

Should be:
 Code:
$GetMSTSUserProcesses[$Index] = $Processes[$Index]

Top
#193457 - 2009-04-16 09:19 AM Re: RFC on GetMSTSUserProcesses() UDF [Re: Arend_]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
Thanks for pointing it out I and U are next to each other so it's an easy mistake to make.
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#193459 - 2009-04-16 10:02 AM Re: RFC on GetMSTSUserProcesses() UDF [Re: Mart]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
There was an even bigger flaw in the code.
Edited the first post so it has the new updated code and a fixed example script.

Sometimes it is so easy to overlook things that totally screw up your script. \:\/
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#193460 - 2009-04-16 12:13 PM Re: RFC on GetMSTSUserProcesses() UDF [Re: Mart]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
 Quote:
I and U are next to each other
I think that proper grammar is "U and I are next to each other" \:D

G-
_________________________
Actually I am a Rocket Scientist! \:D

Top
#193462 - 2009-04-16 12:29 PM Re: RFC on GetMSTSUserProcesses() UDF [Re: Glenn Barnas]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
LOL
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#193463 - 2009-04-16 01:19 PM Re: RFC on GetMSTSUserProcesses() UDF [Re: Mart]
Benny69 Offline
Moderator
*****

Registered: 2003-10-29
Posts: 1036
Loc: Lincoln, Ne
nice 1 Glenn
_________________________
Wait don't order yet,... get KiXforms Designer .NET 2.0 (Beta)
KiXforms Designer .NET 2.0 (Beta)

Top
#193551 - 2009-04-22 10:44 PM Re: RFC on GetMSTSUserProcesses() UDF [Re: Benny69]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
Been using it in a production environment for almost a week now as part of a script the runs every 30 minutes between 8AM and 10PM to report all users of our terminal server (has to do with what percentage of the costs are for what country/department. It’s a finance thing) and it work like a charm.
I’ll post the final version in the UDF section tomorrow.


Edited by Mart (2009-04-22 10:49 PM)
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
Page 1 of 1 1


Moderator:  Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 476 anonymous users online.
Newest Members
batdk82, StuTheCoder, M_Moore, BeeEm, min_seow
17885 Registered Users

Generated in 0.062 seconds in which 0.024 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org