Ok.. fine… I was interested so I played with it more. Here’s what I came up
with. Enjoy!
import win32ts
protocols = {
win32ts.WTS_PROTOCOL_TYPE_CONSOLE: "Console",
win32ts.WTS_PROTOCOL_TYPE_ICA: "Citrix",
win32ts.WTS_PROTOCOL_TYPE_RDP: "RDP",
}
session_types = {win32ts.WTSConnected: "Connected",
win32ts.WTSActive: "Active",
win32ts.WTSConnectQuery: "Connect Pending",
win32ts.WTSShadow: "Shadowing another session",
win32ts.WTSDisconnected: "Disconnected",
win32ts.WTSIdle: "Idle",
win32ts.WTSListen: "Listening",
win32ts.WTSReset: " Resetting",
win32ts.WTSDown: "Down -- Error",
win32ts.WTSInit: "Initializing"}
server_name = "someserver.mydomain.com"
ts_connection = win32ts.WTSOpenServer(server_name)
for s in win32ts.WTSEnumerateSessions(ts_connection, 1):
if s['WinStationName'] != "Services" and s['State'] != win32ts.WTSListen:
user = win32ts.WTSQuerySessionInformation(ts_connection,
s['SessionId'], win32ts.WTSUserName)
if not user:
user = "No one is logged in"
protocol = win32ts.WTSQuerySessionInformation(ts_connection,
s['SessionId'], win32ts.WTSClientProtocolType)
print(f"Session: {s['SessionId']} - State = {session_types[s['State']]}
--> User: {user} -- Protocol: {protocols[protocol]}")
win32ts.WTSCloseServer(ts_connection)
output:
Session: 1 - State = Active --> User: myusername -- Protocol: RDP
Session: 2 - State = Connected --> User: No one is logged in -- Protocol:
Console
HTH
Steven
From: python-win32 <[email protected]> On
Behalf Of Steven Manross
Sent: Sunday, March 13, 2022 8:26 AM
To: Craig R. Matthews <[email protected]>; [email protected]
Subject: Re: [python-win32] pywin32 question
While I don’t have a huge environment to test in, this seems to work remotely
from my win 10 pc to my Windows Server 2016 which has remote admin RDP enabled…
I would assume it’s the same APIs to talk to a full fledged WTS Server.
Kudos to the internet for having the answer already written down for me… even
if it was in python 2 syntax (what else should I expect from a post in
11/2007?). 😊
https://mail.python.org/pipermail/python-win32/2007-November/006425.html
import win32ts
for s in
win32ts.WTSEnumerateSessions(win32ts.WTSOpenServer("MYSERVER.MYDOMAIN.COM"),1):
if s['State'] == win32ts.WTSActive:
print(f"Session is active: {s}")
output:
Session is active: {'SessionId': 1, 'WinStationName': 'RDP-Tcp#124', 'State': 0}
take a look at win32ts here:
http://timgolden.me.uk/pywin32-docs/win32ts.html
I am guessing you would also want to use “WTSQuerySessionInformation” as well
to get more detailed information about the session, but I will leave that to
you to explore.
I just found this today, so I am no expert on use of this module but it looks
GREAT and I will likely develop something with it myself eventually.
Steven
From: python-win32
<[email protected]<mailto:[email protected]>>
On Behalf Of Craig R. Matthews
Sent: Saturday, March 12, 2022 12:02 AM
To: [email protected]<mailto:[email protected]>
Subject: [python-win32] pywin32 question
I was wondering if there is a way in python to determine the idle time for a
terminal server session as QUERY USER does.
Also, is there a way to get that python code to run for a server other than the
one running the code (as in QUERY USER /SERVER:name)?
_______________________________________________
python-win32 mailing list
[email protected]
https://mail.python.org/mailman/listinfo/python-win32