Methods to logoff DISCONNECT terminal services connections


To totally unlock this section you need to Log-in


Login

Sometimes it could be usefull to force logoff of disconnected users from a Terminal Server or after we've logged on on a system (with RDP) and now we doesn't have more connection slots available.

To fix this behavior we could use some methods, as explained below.

Group Policy

On a Windows Server 2003 we could use the Group Policy - Computer Config->Admin Templates->Windows Components->Terminal Services->Terminal Server->Session Time Limits.

Script to logoff DISCONNECT terminal services connections

Methods to logoff DISCONNECT terminal services connections

On a Windows Server 2008 or Windows 7 you will go to Remote Desktop Session Host:

Script to logoff DISCONNECT terminal services connections

Methods to logoff DISCONNECT terminal services connections

By setting this setting: "Set time limit for disconnected sessions" to 1 hour, for example, this configuration will terminate a disconnected session when time limits will be reached to Enabled. It will automatically log them out without scripting it.

User Group Policy Settings for Terminal Server Session Time limits

The following settings are the most common options to configura Terminal Server to handle the remote sessions:

Methods to logoff DISCONNECT terminal services connections

Methods to logoff DISCONNECT terminal services connections

Terminal Services Configuration

To run Terminal Services Configuration, go to Start Menu->Programs->Administrative Tools, click on the Terminal Services Configuration.

In Windows 2003 and earlier, this tool can be run directly as TSCC.msc.  In Longhorn Server, it's called TSConfig.msc.

To access the connection timeout settings, double click on RDP-Tcp.  When the "RDP-tcp Properties" dialog box shows up, click on the Sessions tab, and you'll see the following (the settings are within the red rectangle):

Methods to logoff DISCONNECT terminal services connections

Methods to logoff DISCONNECT terminal services connections

In Windows Server 2003, the dialog looks like this:

Methods to logoff DISCONNECT terminal services connections

Methods to logoff DISCONNECT terminal services connections

By default, TS allows TS sessions to remain for an unlimited amount of time. To change the default check the "Override User Settings" check box, then select the desired time limit from the drop down boxes. To save settings, click the Apply or Ok button.

In Win2k3 and earlier, TSCC.msc can be used for local server (localhost) only.

In Longhorn Server, TSConfig.msc is able to target to a remote server and perform configuration remotely. To target to a remote server, select the "Connect to Terminal Server" action in Actions pane. If the actions pane is hidden, click on View->Customize... menu, and check the "Action pane" check box to show the Actions Pane.

WMI

The time limit settings are exposed in the class template Win32_TSSessionSetting. To check the settings, you can use the read-only properties ActiveSessionLimit, DisconnectedSessionLimit and IdleSessionLimit. These properties return the time limit in milliseconds.

To override time limit user settings (this setting corresponds to the TSCC/TSConfig "Override user setting" for session limit check box), you can use the read/write TimeLimitPolicy property.

To change the time limit, you can use the TimeLimit method.

The read-only property BrokenConnectionAction shows the current setting for action TS will take when the time limit is reached.

To override broken connection user settings (this setting corresponds to the TSCC/TSConfig "Override user setting" for the "When session limit is reached or connection is broken" check box), you can use the read/write BrokenConnectionPolicy property.

To tell TS what to do when the time limit is reached for Active and Idle sessions, you can use the BrokenConnection method.

For example, in VBS (Visual Basic Script), if you want to change the active session limit to 30 minutes, and end the connection when this time limit is reached:

Set obj = GetObject("winmgmts:

{authenticationLevel=PktPrivacy}!root\cimv2\TerminalServices:Win32_TSSessionSetting.TerminalName='RDP-Tcp'")
wscript.echo "Override user setting " & obj.TimeLimitPolicy
wscript.echo "Active session limit is (ms) " & obj.ActiveSessionLimit
wscript.echo "Action is " & obj.BrokenConnectionAction
' set overrride user setting
obj.TimeLimitPolicy = 0
obj.put_
' set timme limit
obj.TimeLimit "ActiveSessionLimit", 1800000
obj.refresh_
' set action
obj.BrokenConnectionPolicy = 0
obj.put_
obj.BrokenConnection 1
obj.refresh_
wscript.echo "Override user setting " & obj.TimeLimitPolicy
wscript.echo "Active session limit is (ms) " & obj.ActiveSessionLimit
wscript.echo "Action is " & obj.BrokenConnectionAction

Note the GetObject method, we can use the property with Key qualifier (TerminalName='RDP-Tcp') to query the object instance we want. This is particularly helpful if you have multiple connections (such as RDP-Tcp, eHome, etc.) in your TS server.

Similarly, we can use the wmi moniker feature to simplify the remote case:

Set obj = GetObject("WinMgmts:{impersonationLevel=impersonate, authenticationLevel=PktPrivacy}" _

& "!\\RemoteServer\root\cimv2\TerminalServices:Win32_TSSessionSetting.TerminalName='RDP-Tcp'")
wscript.echo "Override user setting " & obj.TimeLimitPolicy
wscript.echo "Active session limit is (ms) " & obj.ActiveSessionLimit
wscript.echo "Action is " & obj.BrokenConnectionAction
' set override user setting
obj.TimeLimitPolicy = 0
obj.put_
' set timme limit
obj.TimeLimit "ActiveSessionLimit", 1800000
obj.refresh_
' set action
obj.BrokenConnectionPolicy = 0
obj.put_
obj.BrokenConnection 1
obj.refresh_
wscript.echo "Override user setting " & obj.TimeLimitPolicy
wscript.echo "Active session limit is (ms) " & obj.ActiveSessionLimit
wscript.echo "Action is " & obj.BrokenConnectionAction

The Gallery

SOURCE

LINK

LANGUAGE
ENGLISH

1 thought on “Methods to logoff DISCONNECT terminal services connections”

Comments are closed.