Home > Tech Tips > ‘The terminal server has exceeded the maximum number of allowed connections.’ when trying to remote connect to the server

‘The terminal server has exceeded the maximum number of allowed connections.’ when trying to remote connect to the server

To query and list the sessions on the remote session, you could use QUser.exe or QWinsta, or Powershell.

Powershell is my usual weapon of choice . .

PSTerminalServices is a PowerShell module that helps you manage Terminal Services (including Remote Desktop connections) sessions and processes.

The module is based on an open source project named Cassia (version 2.0.0.60), a .NET library for accessing the native Windows Terminal Services API.

You’ll need to download the module from  http://code.msdn.microsoft.com/PSTerminalServices and then ‘Import’ the module

What you can do with it

The following operations are supported on local and remote computers:

  • Enumerating terminal sessions and reporting session information including connection state, user name, client name, client display details, client-reported IP address, and client build number.
  • Logging off a session.
  • Disconnecting a session.
  • Displaying a message box in a session.
  • Enumerating all processes or processes for a specified session.
  • Killing a process.

Supported Platforms

Cassia has been tested on Windows Server 2008 R2 beta (RTM?), Windows Server 2008, Windows Server 2003, Windows XP, and Windows Server 2000. It should work on Windows Vista as well.

Functions

The following functions are added to the current session when you import the module:        Disconnect-TSSession – Disconnects any attached user from the session.

Get-TSCurrentSession – Provides information about the session in which the current process is running.

Get-TSServers – Enumerates all terminal servers in a given domain.

Get-TSProcess – Gets a list of processes running in a specific session or in all sessions.

Get-TSSession – Lists the sessions on a given terminal server.

Send-TSMessage – Displays a message box in the specified session ID.

Stop-TSProcess – Terminates the process running in a specific session or in all sessions.

Stop-TSSession – Logs the session off, disconnecting any user that might be connected.

Some Code Examples

For more code examples use Get-Help <cmdletName> –Examples.

# Logs off all the active sessions from remote computer 'Labserv07', no confirmations.
PS> Get-TSSession -ComputerName Labserv07 -State Active | Stop-TSSession –Force
# Gets all Active sessions from remote computer 'Labserv07', made from IP addresses that starts with '10'.
PS> Get-TSSession -ComputerName Labserv07 -Filter {$_.ClientIPAddress -like '10*' -AND $_.ConnectionState -eq 'Active'}
# Displays a message box inside all active sessions of computer name 'Labserv07'.
PS>$Message = "Important`n, the server is going down for maintenance in 10 minutes. Please save your work and logoff."
PS> Get-TSSession -State Active -ComputerName Labserv07 | Send-TSMessage -Message $Message
#Gets all processes connected to session ID 0 from remote computer 'Labserv07'.
PS>Get-TSSession -Id 0 -ComputerName Labserv07 | Get-TSProcess

2. QUser

QUser command comes with all the latest Windows clients and servers. This will list the sessions of the remote server (or local machine).

NOTE: if you are using Windows XP, you need to add this location into your System path: C:\Windows\System32\DLLCache. To do this, in command prompt, type the following.

SET PATH = %PATH%;C:\Windows\System32\DLLCache;

QUser help shows,

C:\Anand>QUser /?
Display information about users logged on to the system.

QUERY USER [username | sessionname | sessionid] [/SERVER:servername]

username Identifies the username.
sessionname Identifies the session named sessionname.
sessionid Identifies the session with ID sessionid.
/SERVER:servername The server to be queried (default is current).

E.g.,

C:\>quser /server:LabServer1
USERNAME              SESSIONNAME        ID  STATE   IDLE TIME  LOGON TIME

LabAdmin           rdp-tcp#49          2  Active          .  26/01/2011 1

Logoff the user / session

Logoff command kicks off (logging off) the specified remote session. Log off help shows,

C:\>logoff /?
Terminates a session.

LOGOFF [sessionname | sessionid] [/SERVER:servername] [/V]

sessionname The name of the session.
sessionid The ID of the session.
/SERVER:servername Specifies the Terminal server containing the user
session to log off (default is current).
/V Displays information about the actions performed.

E.g.,

C:\>logoff /server:LabServer1  1 /v
Logging off session ID 1

3. QWinsta

QWinsta is little different and better. It has more features and options. It comes with all flavors of Windows. QWinsta command line help displays as,

C:\>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.

E.g.,

C:\>qwinsta /server:LabServer1

SESSIONNAME       USERNAME                 ID  STATE   TYPE        DEVICE

console                                     0  Conn    wdcon

rdp-tcp                                 65536  Listen  rdpwd

rdp-tcp#2         admvanwyka                1  Active  rdpwd

RWinsta (Logoff the user sessions)

RWinsta has same parameters and does same thing as log off command. It simply means Reset WINdows STAtion. The help goes as,

C:\>RWinsta /?
Reset the session subsytem hardware and software to known initial values.

RESET SESSION {sessionname | sessionid} [/SERVER:servername] [/V]

sessionname Identifies the session with name sessionname.
sessionid Identifies the session with ID sessionid.
/SERVER:servername The server containing the session (default is current).
/V Display additional information.

E.g.,

C:\>RWinsta /Server:LabServer1 1

Note: RWinsta command does not return anything.

Remember – killing an active session will log off the user – so play nice!

There goes – three different command line methods for logging off sessions . . . (of course if you are the GUI type, you could just use The Terminal Services Session Manager)

Advertisements
Categories: Tech Tips Tags: , ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: