Ivan F.
Ivan F.

Reputation: 119

Can I programmatically check a remote computer to see if someone is already connected via Remote Desktop to this computer?

We have a few Windows 10 Pro computers with Remote Desktop enabled. There are "client" machines running Windows 10 Pro and Windows 10 Home.

Is it possible to programmatically check, from a client machine, that the remote computer is already occupied by someone? That is, if another remote client is already connected to it? If so, can I do this without breaking the existing remote connection?

Upvotes: 0

Views: 2294

Answers (2)

RexBarker
RexBarker

Reputation: 1791

You can use the query session command from the command line.

If you're on the same local network as the remote computer, then you can directly use the query session:

Use the command query session /SERVER:<remote pc name, or IP address>

If there is someone actively logged into the console, then it returns console <username> Active as the state. However, this can also mean someone logged in, the session locked, and they walked away for coffee. It doesn't actually mean they are doing something in the session; they are simply logged into the console. This state also occurs if you use another remote login software such as TeamViewer, since it logs in as an active console.

C:\Users\Myself>query session /server:LabServerPC
SESSIONNAME       USERNAME                 ID  STATE   TYPE        DEVICE
services                                    0  Disc
console           LabUser                   1  Active
rdp-tcp                                 65536  Listen

If the computer is already in use by a remote desktop session, it will return it as a session as the active state. In this case rdp-tcp#1 <username> Active.

C:\Users\Myself>query session /server:LabServerPC
SESSIONNAME       USERNAME                 ID  STATE   TYPE        DEVICE
services                                    0  Disc
rdp-tcp#1         LabUser                   1  Active
console                                     3  Conn
rdp-tcp                                 65536  Listen

If you are operating via a VPN, or off the native network, it may be easier to remotely log into the PC using SSH and then query the session directly. In this case, you would use query session only.

Upvotes: 0

Strive Sun
Strive Sun

Reputation: 6289

When a user logs on to a Remote Desktop Services–enabled computer, a session is started for the user. Each session is identified by a unique session ID. Because each logon to a Remote Desktop Connection (RDC) client receives a separate session ID.

Refer: Remote Desktop Sessions

You can use the WTSEnumerateSessions function to retrieve the identifiers of all sessions on a specified RD Session Host server.

WTSEnumerateSessions : Retrieves a list of sessions on a Remote Desktop Session Host (RD Session Host) server.

Note:

  • To enumerate a session, you must enable the query information permission. For more information, see Remote Desktop Services Permissions.
  • To change permissions on a session, use the Remote Desktop Services Configuration administrative tool.
  • To enumerate sessions running on a virtual machine hosted on a RD Virtualization Host server, you must be a member of the Administrators group on the RD Virtualization Host server.

If you want to retrieve the session ID of the current session that the remote desktop service is running, you can call WTSQuerySessionInformation and specify WTS_CURRENT_SESSION for the SessionId parameter and WTSSessionId for the WTSInfoClass parameter.

Upvotes: 1

Related Questions