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

Share the Client Printer in Terminal Services automatically

In this article, I'll discuss how you can use a batch script to share a client's redirected printer automatically. This can help admins deal with program compatibilty problems since some programs always expect a certain printer name.

•The script, which can be downloaded here or at the end of this article.
•Read Microsoft Knowledgebase Article 189105
•A terminal server that you have administrator access to. This script works with Windows 2000 and Windows Server 2003.
•Because this script requires the user to create a printer share, increased privileges may be required. Use of the RunAs command is recommended.

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

Section 1: Headers:

		Set ShareName=RedirPr
		Set UniqueID=1
		Sleep 15

Basically, all I'm doing here is 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. The sleep utility (which is included in the script download) is there just to force the script to wait a little while during the initial logon sequence. Since the terminal server can take a few seconds to map the client printers, the pause is just there to make extra sure that the terminal server is done mapping printers. If the sleep command wasn't there, then we risk having this script run and terminate before the terminal server is completely done mapping printers. If this script wasn't run during the logon, then the sleep command could be removed. The 'Set ShareName=RedirPr' and the 'Set UniqueID=1' commands are telling the script that when we share the printer out, we want its new sharename to be RedirPr_1. The UniqueID makes it so that we don't try to create shares with the same name, which could be possible on a terminal server if many people use this script. If we wanted to change the sharename of the printer, we would just adjust the ShareName variable to be something other than RedirPr.

Section 2: Query the registry:

		"%SystemRoot%\Application Compatibility Scripts\acregl.exe" DefPrint.cmd DefaultPrinter "HKCU\Software\Microsoft\Windows NT\CurrentVersion\Windows" "Device" ""

		If Not Exist DefPrint.cmd (Call :Error 1 1) && (Goto EoF)

		call DefPrint.cmd
		del DefPrint.cmd

		If Not Defined DefaultPrinter (Call :Error 2 1) && (Goto EoF)

This command uses the built in AcRegl.exe command to query the registry for a particlar value. In this case, we are looking for the Device value at HKCU\Software\Microsoft\Windows NT\CurrentVersion\Windows. That value will be set to equal the variable DefaultPrinter in the rest of our script. The If statement checks to make sure the AcRegl.exe command executed properly, and the subsequent call and del commands set the DefaultPrinter variable. If everything has gone right, the DefaultPrinter variable will equal something like: HP DeskJet 820CSe/ComputerName/Session 1,winspool,TS001. The last If statement checks to make sure the DefaultPrinter variable is set; if it isn't then the Error portion of the script script is called and the script quits.

Section 3: Parse the DefaultPrinter variable:

		for /f "tokens=1 delims=," %%i in ('"echo %DefaultPrinter%"') do set PrinterString=%%i

This For command parses the DefaultPrinter variable. It obtains the printer's name only, which will result in: HP DeskJet 820CSe/ComputerName/Session 1. It essentially removes the ',winspool,TS001' portion of the DefaultPrinter variable.

Section 4: Create a unique mapping name:

		for /f %%k in ('net share ^| find /c /i "%ShareName%_%UniqueID%"') do set var=%%k
		if %var% NEQ 0 (set /a UniqueID=%UniqueID%+1) && (goto CheckShare)
		Set ShareName=%ShareName%_%UniqueID%

This small set of commands checks to make sure we have a unique name for our printer mapping. Since this script takes the default printer and shares it, we need to make sure the share name is unique and that we aren't trying to share this printer using a sharename that is already taken.

Section 5: Share the printer:

		rundll32 printui.dll,PrintUIEntry /Xs /n "%PrinterString%" sharename %ShareName% attributes +Shared

The title says it all. The rundll32 command shares the printer out using the sharename that we determined earlier. If everything worked right, then the script will terminate.

Section 6: Cleanup before we exit:

		Goto EoF

		msg %SessionName% /W Your printers were not successfully mapped to the LPT1 port.  Please notify an administrator.  (Error Code: %1 %2)
		Goto EoF


As the script exits, it terminates variable localization using the EndLocal command. However, as stated earlier, if a problem has occurred somewhere in the above script, then the :Error section will be called, which basically just prints out an error message to the user, along with an error code to help an admin troubleshoot the script.

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):