Marko
(Lurker)
2002-03-29 03:54 PM
Session ID number

How can I query the system for session ID number when a terminal services session is on. I need that number to writte a key in the server registry in order to identificate and configurate in the right way, by a script, a shared printer.
The key ends with the session number. Something like that:

HKEY_CURRENT_USER\Printers\DevModePerUser]

"HP LaserJet 4L/MARKO-001/Sesion 3" = hex:48,00,50,00,20,00,4c,00,61,00,73,00,65,\
00,72,00,4a,00,65,00,74,00,20,00,34,00,4c,00,2f,00,41,00,53,00,45,00,52,00,\2d,00,30,00……………………………………………………………………………

In each session this "\session x" change his value. That's is why I need capture the string value.
Anybody can help me?
Thank's a lot


Howard Bullock
(KiX Supporter)
2002-03-29 04:19 PM
Re: Session ID number

Do you have this environment variable available in your client session (WINSTATIONNAME=ICA-tcp#136)?

Is the number after the # what your looking for? If so use instr and substr to extract it.


Howard Bullock
(KiX Supporter)
2002-03-30 05:43 AM
Re: Session ID number

Here is a solution to your ID issue.

There is a tool called "query" on Terminal Servers that can be exploited.
code:
 shell "%comspec% /c query session @userID 1>session.txt"
IF Open(1, "session.txt") = 0
$x = ReadLine(1)
$x = ReadLine(1)
$x = Ltrim(substr($x,instr($x, @UserID) + Len(@UserID)))
$ID = substr($x,1, instr($x, " ")-1)
? $ID
ENDIF



[ 30 March 2002, 05:46: Message edited by: Howard Bullock ]


**DONOTDELETE**
(Lurker)
2002-04-01 05:25 AM
Re: Session ID number

Howard,
Regarding the environment variable idea:

%WinstationName% is only for NT 4.0 TS
The variable was changed to %SessionName% in 2000 TS

In both flavors, the environment variable reports "Console" when you logon from the console. The environment variable's name is somewhat misleading. It is neither the Winstation name nor the Session name. It is actually a combination of the connection name, followed by a pound symbol and then the session ID.

%ClientName% will only be populated if client session (not console session).

The logic should follow:
code:
if @InWin=1
if '%SessionName%' or '%WinstationName%'
; some sort of Terminal Server
if '%ClientName%'
; remote client
else
; logged on from console
endif
else
; not a Terminal Server
endif
else
; Windows 9x
endif



Howard Bullock
(KiX Supporter)
2002-04-01 05:49 AM
Re: Session ID number

I found a case on NT4 TS where the environment did NOT hold up. The session ID reported by the GUI Admin tool was different than what followed the # sign in the environment variable. That is why I then posted the query.exe solution.