Main Webpage

Features

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

Hacks

    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

Scripts

    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

Rename the Client Printer in Terminal Services


In this article, I'll discuss how you can use a batch script to rename the client's redirected printer. This can effectively remove the client's session number from being appended to the end of the name of the printer.

Prerequisites:
•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, currently, only works with Windows 2000.

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

Section 1: Headers:

		SetLocal
		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.

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) && (Goto EoF)

		call DefPrint.cmd
		del DefPrint.cmd

		If Not Defined DefaultPrinter (Call :Error 2) && (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 first 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 that the DefaultPrinter variable was set correctly.

Section 3: Parse the DefaultPrinter variable:

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

These two For commands parse the DefaultPrinter variable. The first For command obtains the printer name and session name, which will result in: HP DeskJet 820CSe/ComputerName/Session 1. The second For command will then parse the DefaultPrinter variable further to obtain just the printer name, which will result in: HP DeskJet 820CSe.

Section 4: Change the name of the printer:

		rundll32 printui.dll,PrintUIEntry /Xs /n "%OldPrinterString%" printername "%PrinterString%"
           

This rundll32 command is the meat of the script. It utilizes the PrintUIEntry function of the printui.dll library to rename the printer. See Microsoft Knowledgebase Article 189105 for more information. At this point, the printer has been renamed.

Section 5: Cleanup before we exit:

		EndLocal
		Goto EoF

		:Error
		msg %SessionName% /W Your printers were not successfully renamed.  Please notify an administrator.  (Error Code: %1)
		EndLocal
		Goto EoF

		:EoF
            

As the script exits, it terminates variable localization. As I said before, this isn't necessary, but it helps to keep things encapsulated. The :Error portion of this script is only called when an error has occurred, like if the DefaultPrinter variable isn't set correctly. The Error codes just print out an error message to the user, along with an error code indicating where in the script the error occurred.

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