Orphaned TS Printing Ports, in the Printing forum on BrianMadden.com
Brian Madden Logo
Your independent source for desktop virtualization, consumerization, and enterprise mobility management.

Orphaned TS Printing Ports, in the Printing forum on BrianMadden.com

rated by 0 users
This post has 8 Replies | 3 Followers

Not Ranked
Points 315
D B Posted: Thu, Oct 12 2006 11:20 AM
Some of you have found when you have a number of users "un-gracefully" logging off of terminal server, they have orphaned terminal server printing ports left in the registry. This can be seen by the users when their printer aquires mulitple sessions in its name (i.e. HPLJ5 on JoeBlow (from CPU) in session 5 ,191).

The ts printer ports can be found in the registry here:
HKLM\SYSTEM\CurrentControlSet\Control\DeviceClasses\{28d78fad-5a12-11d1-ae5b-0000f803a8c2}\##?#Root#RDPDR#0000#{28d78fad-5a12-11d1-ae5b-0000f803a8c2}\"

If a port is orphaned, it will have an key\value "\#ts00n\Device Paramters\Port Description: Inactive TS Port"

This script is set to run from the server itself, but with some tweaking it can be made to run remotely i suppose.

Also keep in mind i am an sysadmin and by no means consider myself a programmer/coder/scriptor by any means.






Const HKEY_LOCAL_MACHINE = &H80000002
strComputer = "."

Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
strComputer & "\root\default:StdRegProv")
Set WshShell =CreateObject("WScript.Shell")
i=1
RegPre1 = "SYSTEM\CurrentControlSet\Control\DeviceClasses\{28d78fad-5a12-11d1-ae5b-0000f803a8c2}\##?#Root#RDPDR#0000#{28d78fad-5a12-11d1-ae5b-0000f803a8c2}\#TS00"
do until i=9
strKeyPath = RegPre1 & i & "\Device Parameters\"
strDelPath = RegPre1 & i
strDelFullPath1 = "HKEY_LOCAL_MACHINE\" & RegPre1 & i & "\Control\"
strDelFullPath2 = "HKEY_LOCAL_MACHINE\" & RegPre1 & i & "\Device Parameters\"
strDelFullPath3 = "HKEY_LOCAL_MACHINE\" & RegPre1 & i & "\"
strValueName = "Port Description"
strSession = i
oReg.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue
if strValue = "Inactive TS Port" then
Wscript.Echo strDelFullpath
Wscript.Echo strSession & "- Port Description Value: " & strValue
'oReg.DeleteKey HKEY_LOCAL_MACHINE, strDelPath
WshShell.RegDelete strDelFullPath1
WshShell.RegDelete strDelFullPath2
WshShell.RegDelete strDelFullPath3
End if
i=1+i
loop

i=10
RegPre2 = "SYSTEM\CurrentControlSet\Control\DeviceClasses\{28d78fad-5a12-11d1-ae5b-0000f803a8c2}\##?#Root#RDPDR#0000#{28d78fad-5a12-11d1-ae5b-0000f803a8c2}\#TS0"
do until i=99
strKeyPath = RegPre2 & i & "\Device Parameters\"
strDelPath = RegPre2 & i
strDelFullPath4 = "HKEY_LOCAL_MACHINE\" & RegPre2 & i & "\Control\"
strDelFullPath5 = "HKEY_LOCAL_MACHINE\" & RegPre2 & i & "\Device Parameters\"
strDelFullPath6 = "HKEY_LOCAL_MACHINE\" & RegPre2 & i & "\"
strValueName = "Port Description"
strSession = i
oReg.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue
if strValue = "Inactive TS Port" then
'Wscript.Echo strDelFullpath
'Wscript.Echo strSession & "- Port Description Value: " & strValue
WshShell.RegDelete strDelFullPath4
WshShell.RegDelete strDelFullPath5
WshShell.RegDelete strDelFullPath6
End if
i=1+i
loop

i=100
RegPre3 = "SYSTEM\CurrentControlSet\Control\DeviceClasses\{28d78fad-5a12-11d1-ae5b-0000f803a8c2}\##?#Root#RDPDR#0000#{28d78fad-5a12-11d1-ae5b-0000f803a8c2}\#TS"
do until i=999
strKeyPath = RegPre3 & i & "\Device Parameters\"
strDelPath = RegPre3 & i
strDelFullPath7 = "HKEY_LOCAL_MACHINE\" & RegPre3 & i & "\Control\"
strDelFullPath8 = "HKEY_LOCAL_MACHINE\" & RegPre3 & i & "\Device Parameters\"
strDelFullPath9 = "HKEY_LOCAL_MACHINE\" & RegPre3 & i & "\"
strValueName = "Port Description"
strSession = i
oReg.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue
if strValue = "Inactive TS Port" then
'Wscript.Echo strDelFullpath
'Wscript.Echo strSession & "- Port Description Value: " & strValue
WshShell.RegDelete strDelFullPath7
WshShell.RegDelete strDelFullPath8
WshShell.RegDelete strDelFullPath9
End if
i=1+i
loop



  • | Post Points: 35
Not Ranked
Points 35
This post really saved my bacon. Turns out, if this is left unchecked on Windows 2000, eventually the system registry file will grow beyond 10Mb, and the computer will crash on bootup with the dreaded "systemced" error. I knew that the registry was huge, I just didn't know why. When I took the registry files to a different system and looked at this key, I found tens of thousands of these dead printers. I just inherited a bunch of really old TS boxes, still being actively used by our offshore developers. These guys all seem to disconnect several times a day, and they have a lot of printers mapped on their local machines.

In thanks, I have re-written the script to be much more efficient, and a little bit more graceful. Some of my dead printers didn't have the Control subkey, so the script was crashing. This script handles either case by just recursively deleting all subkeys.

Const HKEY_LOCAL_MACHINE = &H80000002
CONST BASEKEY = "SYSTEM\CurrentControlSet\Control\DeviceClasses\{28d78fad-5a12-11d1-ae5b-0000f803a8c2}\##?#Root#RDPDR#0000#{28d78fad-5a12-11d1-ae5b-0000f803a8c2}"
CONST VALUENAME = "Port Description"

strComputer = "."

Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
strComputer & "\root\default:StdRegProv")
Set WshShell =CreateObject("WScript.Shell")

oReg.EnumKey HKEY_LOCAL_MACHINE, BASEKEY, arrDeviceKeys

If IsArray(arrDeviceKeys) then
For Each strDeviceKey in arrDeviceKeys
WScript.Echo "Checking : " & strDeviceKey
strParametersPath = BASEKEY & "\" & strDeviceKey & "\Device Parameters"
'WScript.Echo strParametersPath
oReg.GetStringValue HKEY_LOCAL_MACHINE,strParametersPath,VALUENAME,strValue
'WScript.Echo " Port Description : " & strValue

if strValue = "Inactive TS Port" then
strDevicePath = BASEKEY & "\" & strDeviceKey
'WScript.Echo " Deleting :" & strDevicePath
WScript.Echo " Deleting : " & strDeviceKey
DeleteSubKeys HKEY_LOCAL_MACHINE, strDevicePath
end if
Next
End If

'DeleteSubkeys copied (and slightly modified) from http://www.microsoft.com/technet/technetmag/issues/2006/08/ScriptingGuy/default.aspx
Sub DeleteSubkeys(HKEY_LOCAL_MACHINE, strKeyPath)
oReg.EnumKey HKEY_LOCAL_MACHINE, strKeyPath, arrSubkeys
If IsArray(arrSubkeys) Then
For Each strSubkey In arrSubkeys
DeleteSubkeys HKEY_LOCAL_MACHINE, strKeyPath & "\" & strSubkey
Next
End If

'WScript.Echo " Delete : " & strKeyPath
oReg.DeleteKey HKEY_LOCAL_MACHINE, strKeyPath
End Sub
  • | Post Points: 35
Not Ranked
Points 20
There seems to be some inaccurate characters in the script in your post, can you point me to the clean code of your script, and also, how do you run this script?
I assume its just save as a .vbs file and run it, but there is many errors when trying to use a C&P from your post, so maybe just errors in formatting due to pasting into a forum.
Please advise and thanks for your contribution, I have a TS that has many of these orpaned TS ports and its not feasible to restart it, hoping this script will resolve.
  • | Post Points: 20
Not Ranked
Points 25

I have been unable to run this script. I tried saving as .vbs and it wouldn't run at all.

I need to cleanup these ports and then, i guess, restart the print spooler service.

Other than a server reboot, is there any other way to drop these ports or keep them from even being created?

  • | Post Points: 20
Not Ranked
Points 20
oarc replied on Mon, May 16 2011 1:09 PM

Stumbled across this post and had trouble getting the script to run on 2008R2 - it turns out that the registry location has changed. All it takes is changing one line from J Frankenfield's revision (and running the script with admin priveleges) and it works like a dream.

The line

CONST BASEKEY = "SYSTEM\CurrentControlSet\Control\DeviceClasses\{28d78fad-5a12-11d1-ae5b-0000f803a8c2}\##?#Root#RDPDR#0000#{28d78fad-5a12-11d1-ae5b-0000f803a8c2}" 

becomes

CONST BASEKEY = "SYSTEM\CurrentControlSet\Control\DeviceClasses\{28d78fad-5a12-11d1-ae5b-0000f803a8c2}\##?#Root#RDPBUS#0000#{28d78fad-5a12-11d1-ae5b-0000f803a8c2}" 

  • | Post Points: 20
Not Ranked
Points 10

oarc,

 

Do you know how to suppress the Checking and Deleting prompts?  If it is possible.

  • | Post Points: 5
Not Ranked
Points 10

Nevermind, that was too simple.  

'

 

'

  • | Post Points: 5
Not Ranked
Points 5
  • | Post Points: 5
Not Ranked
Points 20
Solly replied on Fri, Aug 3 2012 2:48 AM

Thanks for the solution

  • | Post Points: 5
Page 1 of 1 (9 items) | RSS