'This script addresses the problem of applications running on a Terminal Server/Remote Desktop Session Host
'with long redirected session printer names.
'This script runs as an Active Directory Logon Script.
'When adding the Logon Script in Active Directory, place this script in the standard location when clicking on Browse for the Script Name field.
'Either \\<internal.local, etc.>\SysVol\<internal.local, etc.>\Policies\{some policy number unique to your domain}\User\Scripts\Logon
'OR \\<Domain Controller>\NetLogon
'NEXT, in the Script Parameters: field, place your elevated credentials in the following format Domain\username password.
'ALL Credit goes to Rob Sampson from Australia for his assistance in developing this script. Greg Shepherd, NetManaged
If WScript.Arguments.Count <> 2 Then WScript.Quit
WScript.Sleep 30000
strDomainAdmin = WScript.Arguments.Item(0)
strDomainPW = WScript.Arguments.Item(1)
Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20
'Add a separate line below for each printer to detect the significant part of the name then rename to a new name.
'For Example: objDictionary.Add "OldPrinterName", "NewPrinterName"
Set objDictionary = CreateObject("Scripting.Dictionary")
objDictionary.Add "CFC-NDPtr1", "CFCNDPTR1"
objDictionary.Add "IMC-FDPtr1", "IMCFDPTR1"
objDictionary.Add "IMC-FDPtr2", "IMCFDPTR2"
'This section checks for the default printer of the currently logged on user,
'checks the Dictionary List above and, if there is a match runs the Sub named SetRENRDRPTR
Set objScriptExec = CreateObject("WScript.Shell")
Set objNetwork = CreateObject("WScript.Network")
Set objWMIService = GetObject("winmgmts:\\.\root\CIMV2")
Set colItems = objWMIService.ExecQuery("SELECT Name FROM Win32_Printer WHERE Default=True", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly)
For Each objItem In colItems
For Each strOldName In objDictionary
If InStr(objItem.Name,strOldName) <> 0 Then SetRENRDRPTR objItem.Name, objDictionary(strOldName)
Next
Next
WScript.Quit
Sub SetRENRDRPTR(strPrinterName, strNewName)
'Detects the currently logged on user, the user's default printer and runs psexec with elevated credentials
'to set Ownership of the user's default printer to the currently logged on domain user.
Set objShell = CreateObject("WScript.Shell")
objShell.Run "C:\Support\psexec -accepteula -i -u " & strDomainAdmin & " -p " & strDomainPW & " \\" & objNetwork.ComputerName & " C:\Support\SetACL\x64\SetACL.exe -on """ & strPrinterName & """ -ot prn -actn setowner -ownr ""n:" & objNetwork.UserDomain & "\" & objNetwork.UserName &";""", 0, True
'Sets the Manage Printers option of the printer we just changed OWner on, but MUST be run in the currently logged on user's security context.
'Psexec with elevated credentials does NOT work for this action.
objShell.Run "C:\Support\SetACL\x64\SetACL.exe -on """ & strPrinterName & """ -ot prn -actn ace -ace ""n:" & objNetwork.UserDomain & "\" & objNetwork.UserName & ";p:man_printer""", 0, True
'NOW we rename the printer from the Dictionary List and preserve the visible redirected printer session mapping number.
Set colItems = objWMIService.ExecQuery("SELECT Name,Default FROM Win32_Printer WHERE Name='" & strPrinterName & "'", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly)
For Each objPrinter In colItems
If InStr(LCase(objPrinter.Name), "redirected") > 0 Then
strNumber = Left(Mid(objPrinter.Name, InStrRev(objPrinter.Name, " ")), Len(Mid(objPrinter.Name, InStrRev(objPrinter.Name, " "))) - 1)
strNewName = strNewName & " (" & strNumber & ")"
End If
blnDefault = objPrinter.Default
objPrinter.RenamePrinter(strNewName)
Next
'Now we reset the renamed printer as the default again (Renaming loses the Default Printer setting and we have to reset it).
If blnDefault = True Then
Set colItems = objWMIService.ExecQuery("SELECT Name FROM Win32_Printer WHERE Name='" & strNewName & "'", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly)
For Each objPrinter In colItems
objPrinter.SetDefaultPrinter()
Next
End If
End Sub
In TestVBS1.vbs I have this:
Open in new window
and in TestVBS2.vbs I have this:
Open in new window
and in TestVBS3.vbs I have this, although just for visual testing. Yours should be silent if possible.
Open in new window
Regards,
Rob.