Automate Changes to Terminal Services User Settings

Let this script do the tedious work for you

Dick Lewis

June 13, 2004

6 Min Read
ITPro Today logo in a gray background | ITPro Today


One of the best uses for scripting is to make bulk changes to computer or user settings. Modifying such settings manually is tedious and poses the risk of introducing errors on your users' systems. In addition, you typically must make such changes within a limited time frame. In this article, I provide a script, WTSSetUserInfo, which automates changes to user account settings for Windows 2000 Server Terminal Services. The next time you must change a server path or enable Terminal Services logons for a large group of users, you'll have a script that quickly makes the needed changes.

WTSSetUserInfo uses a freeware utility called SystemTools Terminal Server Command (TScmd) that lets you query or modify all 18 Terminal Services settings. Download TScmd at http://www.systemtools.com/free.htm, and copy the tool to a location with no spaces in the path (e.g., D:systoolstscmd .exe). To display TScmd's online Help, open a command-shell window and type the command

tscmd.exe /?

You'll see a list of the 18 Terminal Services settings that you can change.

The basic syntax that you use to gather information about Terminal Services settings with TScmd is

tscmd.exe  ServerName UserID  Setting

where ServerName is the name of the domain controller (DC) on which the user information resides, UserID is the name of the user whose Terminal Services settings you're accessing, and Setting is one of the 18 possible settings. For example, to gather current values for the setting TerminalServerProfilePath, you run the command

tscmd.exe  DC35 ASmith  TerminalServerProfilePath

The basic syntax you use to modify a Terminal Services setting is

tscmd.exe  ServerName UserID  Setting [NewValue]

where NewValue is the new value for the specified setting. For example, to modify the TerminalServerProfilePath setting, you run the command

tscmd.exe  DC35 ASmith  TerminalServerProfilePath  \profilesrvrwtsprofilesASmith

You can find more information about the command syntax for TScmd in the readme.txt file that comes with the tool.

Before you make large-scale changes to a group's settings as you'll do with WTSSetUserInfo, you should obtain a snapshot of the current settings. You don't want to overwrite settings, then discover you have no record of the previous settings should you need to restore them. Ideally, you should have a mechanism for falling back to the previous settings. WTSSetUserInfo first captures the premodified Terminal Services settings in a comma-separated value (CSV) Microsoft Excel spreadsheet file. After changing the settings, the script again captures the results in the spreadsheet file. Armed with this comparative information, you can determine whether the setting changes occurred as you intended. (In the sidebar "Testing for Success," page 4, I provide techniques that you can use to determine whether a change operation has run successfully.) You'll also have the information you need to restore the previous settings, if necessary.

WTSSetUserInfo Nuts and Bolts
Listing 1 shows WTSSetUserInfo, which consists of three similar subroutines: Checkem1, Setem, and Checkem2. These subroutines are called from the For command, which callout A in Listing 1 shows. This For command parses a text file that contains the user IDs.

WTSSetUserInfo queries and modifies four Terminal Services settings: AllowLogonTerminalServer, TerminalServerProfilePath, TerminalServerHomeDir, and TerminalServerHomeDirDrive. I chose these four because they're the settings that Terminal Services administrators most frequently use. You can modify WTSSetUserInfo to query and change any other setting simply by adding a line of code to each of the three subroutines. Use the code in WTSSetUserInfo as a sample for creating the code to work with other settings.

The four settings first appear in the Checkem1 subroutine at callout B. Checkem1 first uses TScmd to determine whether the specified user account exists. As the first For command in callout B shows, Checkem1 performs a TScmd query, then pipes the results to the Find command. If the Find command locates the string Error in the results that the first query returns, the script ignores the other queries and setting-change attempts, outputs an error message to the spreadsheet file, and proceeds to the next user account. If the Find command doesn't locate the Error string (i.e., if the user account exists), Checkem1 performs four more TScmd queries to obtain the current values for the four Terminal Services settings. The subroutine captures the results of the queries in four variables: CheckBx1, Ppath1, Hdir1, and Hdrive 1.

After Checkem1 captures the original setting values, the For command in the code at callout A moves flow to the Setem subroutine at callout C, which again invokes TScmd—this time, to change the values of the four Terminal Services settings. In the Setem subroutine, notice that the TScmd commands are similar to those in the Checkem1 subroutine except for the addition of a value at the end of each command.

As the code at callout C demonstrates, some Terminal Services settings require number arguments; others require variables. In the command

%TScmdloc% %DC% %user%  AllowLogonTerminalServer 1

specifying the value of 1 for the AllowLogonTerminalServer argument selects the Allow Logon to Terminal Server check box on the Terminal Services UI; a value of 0 clears the check box. For more information about number arguments, see the TScmd tool's readme.txt file.

After Setem changes the settings, the For command at callout A calls the Checkem2 subroutine, which captures the four new setting values. The final line of code in Checkem2 at callout D outputs the variable values to the log file (i.e., the spreadsheet file) specified at the beginning of the script.

Assumptions and Prerequisites
I tested WTSSetUserInfo on Windows XP Professional Edition Service Pack 1 (SP1) and Windows 2000 Server SP3. The script assumes that you've pre-created home profiles and home directories that follow the typical naming convention—that is, they're based on the user's user ID. If you use a different scheme for naming users' home profiles and directories, you must modify the script accordingly. WTSSetUserInfo doesn't create the directories for you, although you can modify it to do so.

To use WTSSetUserInfo, first download TScmd as I directed earlier. After you've downloaded TScmd, download WTSSetUserInfo.bat from the Windows Scripting Solutions Web site at http://www.winnetmag.com/windowsscripting, InstantDoc ID 42736. (Column widths in the printed publication force us to wrap code lines, which might cause the printed code to run incorrectly.)

Follow these steps to use WTSSetUserInfo:

1. Configure TScmd's location at the top of WTSSetUserInfo.bat. In the command

Set TScmdloc=D:systools  tscmd.exe

replace D:systoolstscmd.exe with your path. Don't include spaces in this path or any of the paths in the following commands.

2. Configure the path to the input list, which is a text file consisting of user IDs (one user ID per line). In the command

Set UserList=D:userlist.txt

replace D:userlist.txt with your path.

3. Configure the location of the spreadsheet file. In the command

Set OutputLog=D:log6-4-04.csv

replace D:log6-4-04.csv with the path to the spreadsheet file.

4. Configure the path to the user's home profile (%USERNAME% will be appended to the path). In the command

Set ProPath=  \profilesrvrwtsprofiles

replace \profilesrvwtsprofiles with your path.

5. Configure the drive letter to the Terminal Services home directory. In the command

Set ConnectL=T:

replace T: with your drive letter.

6. Configure the home folder (%USERNAME% will be appended to it). In the command

Set HFolder=\fileserver2

replace \fileserver2 with your home folder.

7. Configure the DC on which you want to use WTSSetUserInfo to change Terminal Services settings. In the command

Set DC=DC35

replace DC35 with the name of your DC.

Now you're all set to run WTSSetUserInfo. This handy script can improve the accuracy and speed with which you can make changes to the Terminal Services user account settings in your environment. Let WTSSetUserInfo do the tedious work for you.

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like