Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Creating printers with a script

Posted on 2008-10-09
Medium Priority
Last Modified: 2012-06-21
I am looking to create printers using a script on logon.  The goal is to have specific printers for certain user groups on 4 terminal servers.  The users connect to these servers through both a Citrix web interface and remote desktop.  We would like to have some of the printers created as a connection to a network printer, and others as a local printer with the port set to the network share.  This is mostly for naming reasons, so people can see 'Printer1' rather than '\\server2\Printer1' which confuses them.
Basically the problem is that I create a printer on the terminal server on one account, and it doesn't show up on any other user accounts.  We need an automated way to ensure that everyone gets the printers they need.

In other words what I want is say Bob in accounting to have the following printers:
Printer1    which is a local printer with it's port set at \\server2\Printer1
\\station1\labelprinter   a normal network share printer
But then I want Susie in sales to have totally different printers, along the same format.

I have tried doing this multiple ways with both rundll32 printui.dll,PrintUIEntry  and cscript prnmngr.vbs commands.
Example of some rundll32 commands I tried:
rundll32 printui.dll,PrintUIEntry /dl /n "Testing" /q
rundll32 printui.dll,PrintUIEntry /if /b "Testing" /f %windir%\inf\ntprint.inf /r "\\server2\Testing" /m "Brother HL-5140 series" /u
rundll32 printui.dll,PrintUIEntry /in /n \\bob\label /q

But then this method ran into the problem that it will recreate the printer every logon, so it must be deleted before being created.  It also has the problem that even though I used the silent switch, it still will show the user an error if it cannot find a network printer.  And sometimes it tries to create the new printer before the old one is deleted (even though the delete comes first in the script) and I end up with Printer1 (Copy 1).  I do like however that this method allows me to create printers on a per machine basis.

Then I tried cscript prnmngr.vbs to acheive my goal.  Here are some examples of what I did:
cscript prnmngr.vbs -a -p "CopyRoomXerox" -m "Xerox WorkCentre 4150" -r "\\server2\CopyRoomXerox"
cscript prnmngr.vbs -ac -p "\\bob\label"
This method had the benefit of not creating additional copies of printers that already exist, or showing error messages to the user.  But it doesn't allow per-machine creation or as much customization as the rundll32 did.

I was considering creating seperate .bat files for each group of printers and adding them to people's Active Directory user profile logon script.  But then this would add the printers on any computer they log into on the network.  Plus, I realized that the scripts fail because the users are not administrators and cannot add printers.

How can I acheive my goal ?  I've seen some things about creating a vbs script myself, but I don't know anything about how to program that or if it would even work for all the things I want.  And are there any graphical interfaces for doing this?  It seems wierd that microsoft allows you to add a printer in 'Printers and Faxes' but there is no way to make it on all user accounts (other than copying that profile to 'Default User' and deleting all user accounts on that machine.)

500 points for anyone that can provide a way to accomplish all of these things.
Question by:blackjack2003
LVL 21

Expert Comment

ID: 22684385
If these commands work for you the best then maybe the additional time to process will be worth it. Maybe not.

Unfortunately, if you watch the logon process and when the spooler actually sets your printers it could be almost up to 1-2 minutes after the desktop appears.  This is why even you have done a delete printer first it really hasn't disappeared until the spooler has removed it.

Things to try:

rundll32 printui.dll,PrintUIEntry /dl /n "Testing" /q
rundll32 printui.dll,PrintUIEntry /if /b "Testing" /f %windir%\inf\ntprint.inf /r "\\server2\Testing" /m "Brother HL-5140 series" /u
rundll32 printui.dll,PrintUIEntry /in /n \\bob\label /q

Another thing you could try is to delete the registry where the testing printer is stored.

LVL 18

Accepted Solution

exx1976 earned 2000 total points
ID: 22704174
If you're using Citrix, then wy not just assign the printers to the users using Citrix Policies?  That's how I curently do it for about 35 printers in my network, and it works great.  Just create security groups for each printer, add the users that should have that printer to the sec group, then in the Citrix Policy, under "Apply to", select that SecGroup, and the printers will get automatically mapped into their sessions.

For the TS piece, I'd do the same thing, create a SecGroup for each printer, and then use VBS to check group membership and assign printers based on the group the logged on user is part of..

This is quick and ugly, but it'll give you a good starting point:

Set oWS = CreateObject("WScript.Shell")
Set oNetwork = CreateObject("WScript.Network")
Set oFS = CreateObject("Scripting.FileSystemObject")
UserName = oWS.ExpandEnvironmentStrings("%USERNAME%")
domain = "mydomain"
UserDN = GetObjectDN(UserName,domain)
Set oGroup = GetObject("LDAP://CN=SecGroup,CN=Users,DC=mydomain,DC=local")
If oGroup.ismember(UserDN) Then oNetwork.AddPrinterConnection "","\\server2\printer1"
Set oGroup = GetObject("LDAP://CN=SecGroup2,CN=Users,DC=mydomain,DC=local")
If oGroup.ismember(UserDN) Then oNetwork.AddPrinterConnection "","\\server2\printer2"

Function GetObjectDN(strObject, strDomain)
      Const ADS_NAME_INITTYPE_GC = 3
      Const ADS_NAME_TYPE_1779 = 1
      Const ADS_NAME_TYPE_NT4 = 3
      Dim objNameTranslate
      Dim strObjectDN
      On Error Resume Next : Err.Clear
      Set objNameTranslate = CreateObject("NameTranslate")
      objNameTranslate.Init ADS_NAME_INITTYPE_GC, ""
      objNameTranslate.Set ADS_NAME_TYPE_NT4, strdomain & "\" & strObject
      strObjectDN = objNameTranslate.Get(ADS_NAME_TYPE_1779)
      If Err.Number <> 0 Then  ' Make the DN Blank for a Failed Search
            strObjectDN = ""
      End If
      Set objNameTranslate = Nothing
      On Error Goto 0
      GetObjectDN = strObjectDN
End Function

Author Closing Comment

ID: 31504791
We ended up using citrix printer profiles and it works great like you said.  The original intention behind the scripts for printers was that we wanted to avoid the server name before each printer.  Instead of having \\server\printer1 we wanted to see just "printer1".  But the ease of Citrix printer profiles convinced us to live with \\server\printer1.
Sorry for taking such a long time to award points, our account was expired.

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

#Citrix #Citrix Policies #XenDesktop #VDI #POC #Citrix Univeral Printer Driver #Citrix UPD
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
How to install and configure Citrix XenApp 6.5 - Part 1. In this video tutorial we have explained step by step installation of Citrix XenApp 6.5 Server on Windows Server 2008 R2 is explained in this video. We have explained the difference between…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question