Working on a XenApp project where we had to deploy an SAP Module, I had to work on renaming the autocreated client printers to remove spaces and shorten names.
To do so I've written a little vbs script using WMI to access the printers and rename them according to the SAP module requirements.
Note : The main purpose of this article is to explain how to run such a script with a standard user's account (without having to do a runas task requesting an admin account).
'------------------------------------------------------------------------------------------------
On Error Resume Next
Set WshShell = WScript.CreateObject("WScript.Shell")
Set ObjFSO = CreateObject("Scripting.FileSystemObject")
Const LogFileName = "RenameAutoCreatedPrinters.Log"
Const PrntNameSize = 8
LogFile = WshShell.ExpandEnvironmentStrings("%USERPROFILE%\" & LogFileName)
Set WshNetwork = WScript.CreateObject("WScript.Network")
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
strUser = WshNetwork.UserName
RenameAutoCreatedPrinters()
Public Sub RenameAutoCreatedPrinters()
on error resume next
err.clear
Set colInstalledPrinters = objWMIService.ExecQuery("SELECT * FROM Win32_Printer",,48)
i = 1
For Each objPrinter in colInstalledPrinters
if inStr(objPrinter.comment,"Auto Created Client Printer") = 1 then
OrigName = objPrinter.Name
NewName = Left(objPrinter.Name,PrntNameSize)
NewName = Replace(NewName,Space(1),"_")
NewName = Replace(NewName,"#","_")
NewName = Replace(NewName,"(","_")
NewName = Replace(NewName,")","_")
objPrinter.RenamePrinter(NewName & "_" & strUser)
writeLog "RenameAutoCreatedPrinters: Printer" & Space(1) & chr(34) & OrigName & chr(34) & Space(1) & "Renamed To" & Space(1) & chr(34) & NewName & "_" & strUser & chr(34)
End If
i = i + 1
Next
If err then
writeLog "RenameAutoCreatedPrinters: ERROR: " & err.number & " -> " & err.description
else
writeLog "RenameAutoCreatedPrinters: OK: "
End If
End Sub
Private Sub WriteLog(Message)
On Error Resume Next
Err.Clear
Set objLogFile = ObjFSO.OpenTextFile(LogFile, 8, True)
objLogFile.Write date & Space(1) & time & Space(1) & Message & VbCrlf
objLogFile.Close
End Sub
'------------------------------------------------------------------------------------------------
There is nothing special in this script and actually that's not the most interesting part of this article.
Why ?
Simply because if you run it at user logon, it'll execute flawlessly and in the log file created in the User's Profile Root all we be ok BUT ... autocreated printers won't be effectively renamed !!!
Strange ... WMI says it is ok but that's not working !
Actually do be able to reach our goal we'll have to focus on XenApp printing subsystem.
Starting with XenApp 4.0 (Citrix Presentation Server), the new print subsystem involves a new service (cpsvc / Citrix Print Manager) that is responsible for creating user's printers.
This new service defines new rights on autocreated printers and the user is not allowed to rename them for example.
Unlike giving servers admin rights on autocreated printers, as far as I know there is no DefaultPrnFlag to allow a user to rename its printers.
That's not a good news, so we have to investigate further ...
Actually, to understand how to achieve an sucessfull printer rename we have to look deeply into the new client printers autocreation process.
The Citrix Print Manager Service is running with a local server account (CtxSmaUser with XenApp 4.0 and CpsvcUser starting with XenApp 4.5).
For the service to work properly, it needs very specific rights and priviledges (there are free tools provided by Citrix in case you have to recreate this account).
These requirements are because all created printers rights are derived from this account. (Now we have a clue !)
The solution to give user's rights on autocreated printers is then to set the appropriate permissions on the Print Manager Service account (either CtxSmaUser or CpsvcUser).
The easiest way is to make the account member of the "Power Users" Group.
If you want to tighten security, you can also give the account only the requested priviledge.
After you add it to the group, restart the service (Warning, do this only when the servers are not in production) and all newly autocreated printers will reflect the new settings.
The script provided at the beggining of this article will then work flawlessly but more important : effectively without any need to do a "RunAs" task or modify any DefaultPrnFlag".