Main Webpage


    Terminal Services Bulletin Board
    Links and Downloads
    Matthew Harris' Resume
    Contact the admin


    Disable the X box on the Terminal Services Client
    Change the client version of the Terminal Services Client
    Add the clock to the taskbar through the registry
    Make all processes appear in the Task Manager through a registry hack
    Prevent disconnects and stabilize your terminal services connection
    Fix your TSAdmin application when it becomes nonfunctional on the taskbar
    Disable/Enable all terminal services logons through the registry


    Restrict users to one session and reconnect them
    Share the redirected printer automatically
    Map your client's printer to an LPT port
    Rename client redirected printers
    Restrict users to only one terminal services session
    Automatically connect disconnected users back to their sessions
    Force software license compliance through a script
    How to reset all your TS sessions at once

Hard to Diagnose Problems

    Incorrect IE permissions can disable opening new IE windows

Automatically Connecting the User to their Disconnected Session

In this article, I'll discuss how you can use a batch script to automatically connect a user to their disconnected session.

•The script, which can be downloaded here or at the end of this article.
•A terminal server that you have administrator access to.

For this tutorial, I'm going to step through the script to explain what happens.

Section 1: Variable declarations and headers:

            Set LoginCount=0
            Set DiscSessNum=-1

Basically, all I'm doing here is declaring some variables, setting them equal to 0, and forcing the script to localize all these variables within this script. Localizing the variables isn't absolutely necessary, but this helps to prevent variables from mixing up between your scripts, especially if you merge this script with other scripts.

Section 2: Query the server and seek out the user:

            Rem Checking the server to see if the user is loggin on twice...
            For /f %%j in ('qwinsta %UserName% ^| find /c /i " %UserName% "') Do Set LoginCount=%%j

The For command runs a QWinsta.exe command, which lists all the current users on the local server. This output of the QWinsta command is then run through the Find command. The Find command will then output the number of times it finds the user's name (%UserName%). This number is stored in the LoginCount variable.

Section 3: Verify the user is logged in twice and disconnected once:

            Rem LoginCount will be equal or greater than 2 if the user is logged in already
            If %LoginCount% EQU 1 Goto eLoginCount
            Rem The user is logged in more than are at least one of their sessions disconnected?
            For /f "skip=1 tokens=2,3" %%i in ('qwinsta %UserName% ^| find /i "disc"') Do (if %%j==disc set DiscSessNum=%%i)
            Rem If DisconnectedSessionNum is a valid number, then let's try to connect to it...
            If %DiscSessNum% EQU -1 Goto eLoginCount

LoginCount's value is now checked. If it equals 1, that means the current user is only logged in once, so the rest of the script can be skipped (Goto eLoginCount). If LoginCount's value is greater than 1, that means the current user is logged in twice. If this is the case, another For loop is run to verify that at least one of the sessions is disconnected. The For loop will run another QWinsta command, and the output is checked to see if any of the user's sessions are in the disconnected state. The session number of the disconnected state (if one exists) is then saved in the variable DiscSessNum. Lastly, DiscSessNum is verified just to make sure that it is a valid number.

Section 4: Connect the user to their disconnected session:

            Rem Check what OS we are running:
            Rem Windows 2000/2003 or NT?
            If Exist %SystemRoot%\System32\TSCon.exe (TSCon.exe %DiscSessNum%) Else (Connect.exe %DiscSessNum%)

Because the connect function was renamed between Windows NT TSE and Windows 2000/2003, we need to figure out which one to use. The TSCon.exe or the Connect.exe utility will both make the current user session attach to the disconnected session. The utility will then leave the current session in the disconnected state.

Section 5: Cleanup before we exit:

            Rem The user should now be connected to the disconnected session.
            Rem The current session will not become disconnected, so let's kill it before we end this script.

As the script exits, it terminates the variable localization for the script. As I said before, this isn't necessary, but it helps to keep things encapsulated. After the TSCon.exe/Connect.exe command runs, it'll leave the current session (which is running this script) in a disconnected state. Running the Logoff.exe command will log the current session off.

Well, that's about it. You've just finished a walkthrough tutorial for this script. tutorial would be complete without a download, so here is the script in its entirety (compressed):