CitrixTools.Net Articles

Current Articles | Categories | Search | Syndication

Programmatically rename XenApp AutoCreated Client Printers

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

posted on Friday, August 14, 2009 9:20 AM by Pierre Marmignon    

Previous Page | Next Page

COMMENTS

Has anyone got this running in a Server 2008 x64 XenApp 5.0 environment?

posted @ Monday, February 01, 2010 5:23 AM by jprins


Nope, haven't tried this on 2008 yet, but I will soon. I'll post when I've tested it on a 2008 x32 first.

posted @ Tuesday, February 16, 2010 9:38 AM by Andreas


I have tried the script, and it works great if the user that is launching the script is a Power User. I call the script before I launch an application.

If the user is a standard user, the printers do not rename, if the user is in Power user group, the printers will rename. I have placed the ctx_cpsvcuser into the power group, and administrators group. i have restared the citrix printer service. Still without the active user in the power users group, printers will not rename.

I am useing server2003 R2, and XenApp 5. I have tested on both client 10 and client 12.
ATL-CCP0407G-Canon3030 on usatlp00 S3 - with power user group.
ATL-CCP0407G-Canon3030 on usatlp00 (from A46274-3844) in session 3 - is without power user group.

What do I need to do to allow standard users to launch this script, and have it rename thier printers? I cannot put over 1500 users into power users group.

posted @ Friday, July 09, 2010 1:19 AM by Charles@Coke


some of the info in this article is not correct for xenApp 6.0 for windows 2008 r2.
per http://support.citrix.com/article/CTX125942 the Citrix Print Manager Service now runs by default with the Local Service account .
has anyone tried to change this and get the above script to work ?

thanks

posted @ Tuesday, February 07, 2012 2:40 PM by mwarril


Only registered users may post comments.