Improve company productivity with a Business Account.Sign Up

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 634
  • Last Modified:

Creating printers with a script

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.
1 Solution
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.

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
blackjack2003Author Commented:
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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now