[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 324
  • Last Modified:

MS Windows Server 2003 Printer Script

I have a Windows 2003 server (not R2). Three user groups: Students, Teachers and Business. There are three primary network printers and several direct attached. I want to have a script which will:

If a Student or Teacher logs into a classroom computer (all classroom computers have names begining with "Grade") then the default printer should be "Upstairs" Other wise they default to "ComputerRoom"

If a Business user logs in they default to their attached printer or can select from: "ComputerRoom" or "Color"

Thanks in advnace for your help!
0
mlsills
Asked:
mlsills
  • 14
  • 7
  • 2
1 Solution
 
dm7941Commented:
Here's how you can do this with a vbs script, via login script, startup folder, or even GP.
Replace the \\server\printer with the appropriate path.
Obvoiusly, I can't test it with all your OU names and printers, so let me know if you get anything weird.  


********************************BEGIN SCRIPT
Option Explicit
Dim objNetwork, strUNCPrinter, strDefPrinter, objAdsSystemInfo, objComputerName, objOU, strGradePrinter, strUpstairsPrinter, strComputerRoomPrinter, strColorPrinter
strGradePrinter="\\Server\printer"
strUpstairsPrinter="\\Server\printer"
strComputerRoomPrinter="\\Server\printer"
strColorPrinter="\\Server\printer"


Set objNetwork = CreateObject("WScript.Network")

'add all printers to computer
objNetwork.AddWindowsPrinterConnection strGradePrinter
objNetwork.AddWindowsPrinterConnection strUpstairsPrinter
objNetwork.AddWindowsPrinterConnection strComputerRoomPrinter
objNetwork.AddWindowsPrinterConnection strColorPrinter

'pick which to set as default
Set objAdsSystemInfo = CreateObject("adsysteminfo")
Set objComputerName = GetObject("LDAP://" & objAdsSystemInfo.ComputerName)
Set objOU = GetObject(objComputerName.Parent)
strOU = replace(objOU.Name,"OU=","")

Select Case strOU
 Case "Students"
      if left(objAdsSystemInfo.ComputerName,5)="Grade" then
            objNetwork.SetDefaultPrinter strUpstairsPrinter
      else
            objNetwork.SetDefaultPrinter strComputerRoomPrinter
      end if      
 Case "Teachers"
      if left(objAdsSystemInfo.ComputerName,5)="Grade" then
            objNetwork.SetDefaultPrinter strUpstairsPrinter
      else
            objNetwork.SetDefaultPrinter strComputerRoomPrinter
      end if      

 Case "Business"

End Select

WScript.Quit


**********************************END SCRIPT

I left the Business OU blank, because what you asked for is essentially to allow the user to always choose.  Since all printers are installed, they can just pick anytime.  That way their default will stay on that machine, unless you use roaming profiles.  If you need to script setting a local printer as default, you can use something like the following:

Note:  this is something I hacked together as an example, it will probably need adjustment.


Set objPrinter = objWshNetwork.EnumPrinterConnections
 For a = 0 To objPrinter.Count - 1 Step 2
   if Left(objPrinter(a),3) = "LPT" Then
     PrinterFlag = True
   Elseif Left(objPrinter(a),6) = "USB001" Then
     PrinterFlag = True
   Else
     PrinterFlag = False
   End If
 Exit For
0
 
mlsillsAuthor Commented:
Thank you for the code! I beleive that it is getting me there, still a problem. I had to make some changes to your code. I probably led to some confusion and apologize in advance if I have added to my own problems in modifying the code your prepared.

There is not "Grade Printer" so I eliminated references to that, the Color Printer is not allowed to be used by students or teachers accounts (they would go wild!) so I eliminated references to that. However, getting an error "strOU not defined"

Further questions:

1: Some of the computers are Windows 2000 is this a potential problem using this?
2: In the classrooms and computer lab we are using a generic user account "suser" so there may be and are multiple logins using the same account, although on different work stations. Will this cause a problem?

The script now looks like the following:
=================================================================================
Option Explicit
Dim objNetwork, strUNCPrinter, strDefPrinter, objAdsSystemInfo, objComputerName, objOU, strUpstairsPrinter, strComputerRoomPrinter
strUpstairsPrinter="\\NHHDSDC3.LOCAL\Upstairs_HP"
strComputerRoomPrinter="\\NHHDSDC3.LOCAL\ComputerRoom_HP"

Set objNetwork = CreateObject("WScript.Network")

'add all printers to computer
objNetwork.AddWindowsPrinterConnection strUpstairsPrinter
objNetwork.AddWindowsPrinterConnection strComputerRoomPrinter

'pick which to set as default
Set objAdsSystemInfo = CreateObject("adsysteminfo")
Set objComputerName = GetObject("LDAP://" & objAdsSystemInfo.ComputerName)
Set objOU = GetObject(objComputerName.Parent)
strOU = replace(objOU.Name,"OU=","")

Select Case strOU
 Case "Students"
      if left(objAdsSystemInfo.ComputerName,5)="Grade" then
            objNetwork.SetDefaultPrinter strUpstairsPrinter
      else
            objNetwork.SetDefaultPrinter strComputerRoomPrinter
      end if      
 Case "Teachers"
      if left(objAdsSystemInfo.ComputerName,5)="Grade" then
            objNetwork.SetDefaultPrinter strUpstairsPrinter
      else
            objNetwork.SetDefaultPrinter strComputerRoomPrinter
      end if      

 Case "Business"

End Select

WScript.Quit

==============================================================================
I'd like to talk about the Business user after I get this working.

Thank you again for your help!
Mark
(my direct e-mail is:  # Removed by ModernMatt 21 Feb 2009 #)
0
 
mlsillsAuthor Commented:
Thank you, I'd not thoughtof the e-mail address problem ...thanks for your help!

Mark
0
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

 
activephotoCommented:
Hi,
you get the strOU is undefined because there is a "Option explicit" statement in the beginning of the script and strOU is not defined in a Dim statement so add that and that error will disappear.
Q> Some of the computers are Windows 2000 is this a potential problem using this?
A> I assume that the groups are groups in ActiveDirectory, and if the domain can handle Windows 2000 computers I dont think there is a problem

Q> In the classrooms and computer lab we are using a generic user account "suser" so there may be and are multiple logins using the same account, although on different work stations. Will this cause a problem?
A> as long as the user account is member of the different groups you described in the question it should be handled as that type of user, regardless of the person behind the keyboard. This script should look for the computer name, logged on user and then map the user to what groups it belongs to and from there deside which printer to connect and which printer to set as default.
In the script I see that the printers are mapped regardless of what user, group that is connected, if you want only some groups to be able to print in e.g. the color printer you need to move the objNetwork.AddWindowsPrinterConnection  statment into the If clauses that desides what group you belongs to?
hope you understand what described , otherwise we give it another round...
Good luck!
/ Mikael
0
 
mlsillsAuthor Commented:
Hello Mikael,

Many thanks for your thoughtful reply, it goes a really long way to helping me out of this problem!

I am afraid that I need more help with where or what to do with the strOU statement.Another person on the Experts Exchange sent this script to me in response to my initial question. So I don't know if the strOU statement is necessary or where it should actually be laced.

After the error is corrected I think that I need to find a way to remove all the currently installed printers so that only this script is in control.

Thank you for your continuing help!

Mark
0
 
activephotoCommented:
From what I can see in the script the strOU is the groupname you want to decide if its a teacher, student or business and thats whats used in the Select Case statement to do things depending on what the group is.
There is a command to delete printer connections as well from a computer, and that is
WshNetwork.RemovePrinterConnection \\printserv\DefaultPrinter, true, true
You can find the reference for it at http://msdn.microsoft.com/en-us/library/tsbh2yy7(VS.85).aspx
So what you probably need to do is to loop through all connected printers and delete all connected printers (see code snippet below), but you should take into consideration that a user might have been mapping a printer by
them self that they want/need to have.
/ Mikael


' Script part to remove all network connected printers on the local computer
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colInstalledPrinters = objWMIService.ExecQuery ("Select * From Win32_Printer Where Network = True")
For Each objPrinter in colInstalledPrinters
    objPrinter.Delete_
Next

Open in new window

0
 
dm7941Commented:
Sorry for the delay getting back to you, mlsills.  Just after I put that code in for you, i took a short vacation.

Anyway, the strOU is just a string variable.  It should have been dimentioned in the second line of code:
Dim objNetwork, strUNCPrinter, strDefPrinter, objAdsSystemInfo, objComputerName, objOU, strUpstairsPrinter, strComputerRoomPrinter
should be
Dim objNetwork, strUNCPrinter, strDefPrinter, objAdsSystemInfo, objComputerName, objOU, strUpstairsPrinter, strComputerRoomPrinter, strOU

That is just a holder for the name of the OU that is returned by the line:
strOU = replace(objOU.Name,"OU=","")

this line gets the whole name of the OU (OU=OUNAME) and cuts off the "OU="  That makes it easier to deal with the next section which does all the IF stuff with the OUs.  

I'd be wary of removing all printers from a user's login.  Network printers are saved on a per-user basis, not per-machine.   That means the only way another printer would be mapped is manually for a specific user.  If you remove ALL printers, you could be removing a specific printer that is really needed by a user.  

I've got no more vacation any time soon, so i'll keep checking back here in case you need follow up.
0
 
mlsillsAuthor Commented:
Thanks and welcome back! Good to be communicating with the one who wrote the code. I'm not at school now and neglected to bring a copy of the modified script with me. But I will attend to that first thing in the morning ... probably with more questions!

Thanks again!

Mark
0
 
mlsillsAuthor Commented:
Good morning ... at least it's morning here in Connecticut!

Here is the code, modified from your original.
===================================================================================
Option Explicit
Dim strOU, objNetwork, strUNCPrinter, strDefPrinter, objAdsSystemInfo, objComputerName, objOU, strUpstairsPrinter, strComputerRoomPrinter
strUpstairsPrinter="\\NHHDS3.NHHDSDC3.LOCAL\Upstairs_HP"
strComputerRoomPrinter="\\NHHDS3.NHHDSDC3.LOCAL\ComputerRoom_HP"
Set objNetwork = CreateObject("WScript.Network")
'add all printers to computer
objNetwork.AddWindowsPrinterConnection strUpstairsPrinter
objNetwork.AddWindowsPrinterConnection strComputerRoomPrinter
'pick which to set as default
Set objAdsSystemInfo = CreateObject("adsysteminfo")
Set objComputerName = GetObject("LDAP://" & objAdsSystemInfo.ComputerName)
Set objOU = GetObject(objComputerName.Parent)
strOU = replace(objOU.Name,"OU=","")
Select Case strOU
 Case "Students"
      if left(objAdsSystemInfo.ComputerName,5)="Grade" then
            objNetwork.SetDefaultPrinter strUpstairsPrinter
      else
            objNetwork.SetDefaultPrinter strComputerRoomPrinter
      end if      
 Case "Teachers"
      if left(objAdsSystemInfo.ComputerName,5)="Grade" then
            objNetwork.SetDefaultPrinter strUpstairsPrinter
      else
            objNetwork.SetDefaultPrinter strComputerRoomPrinter
      end if      
 Case "Business"
End Select
WScript.Quit
================================================================================
I would generally agree with your comment to not delete existing printers, however, students and teachers should have no choice, we want them to only have the approved printers and to default to the printer closest to them. However, that should be a one-time script and I'd like to address that later.

I have tested the script (using it with student and teacher accounts) With the teacher and student accouts in the lab the printer is defaulting to the "Upstairs_HP" and it should default to the "ComputerRooom_HP".

In the classrooms, where the OS is Win 2000 It does not appear to be working, the problem is that users get a message: "Do not have suffieient security priovelegs to install devices." Local machine policies have Restrict Install Printer Drivers  - disabled so that should not be a problem.

I really would appreciate your thoughts and suggestions!

Mark
0
 
dm7941Commented:
There is another way to map network printers using the printUI:
rundll32 printui.dll,PrintUIEntry /in /n\\NHHDSDC3.LOCAL\Upstairs_HP

Try this command (from the console) on one of the classroom PCs and let me know if it works.  There is another policy that might be stopping it.  It's the "Load and unload device drivers" user right.  I realize you might not want to enable that for your students, but for testing purposes, can you temporarily change that?

The incorrect default is because I used objAdsSystemInfo.ComputerName instead of objNetwork.ComputerName in the IF statements.  The objADSSysteminfo gives the full AD computer name.  

Here is the corrected part:
Select Case strOU
 Case "Students"
      if left(objNetwork.ComputerName,5)="Grade" then
            objNetwork.SetDefaultPrinter strUpstairsPrinter
      else
            objNetwork.SetDefaultPrinter strComputerRoomPrinter
      end if      
 Case "Teachers"
      if left(objNetwork.ComputerName,5)="Grade" then
            objNetwork.SetDefaultPrinter strUpstairsPrinter
      else
            objNetwork.SetDefaultPrinter strComputerRoomPrinter
      end if      
 Case "Business"
End Select

Give that a shot and let me know how it goes.

Thanks!
0
 
mlsillsAuthor Commented:
This almost makes me want to go back to school right now and give it a try!

I'll let you know how things go in the morning!

Thanks,

mark
0
 
mlsillsAuthor Commented:
Hello dm7941,

I regret to say that the change of code did not work, in all cases it defaults to the "UpstairsPrinter".

I tried the rundll32 from one of the classroom ("Grade") computers, using "Start" "Run" and that resulted in an error ... unable to locate the printer.

I did modify the rights to permit "Load and unload device drivers: and that did not have any effect either.
I am really at a loss to understand what the problem is ...

Mark
0
 
dm7941Commented:
The OU and Computer name statements are case sensitive, i.e "Students" vs "STUDENTS"

You can do a little error checking with the msgbox command like this:
msgbox ("OU = " & strOU)
msgbox("Computer Name = " & objNetwork.ComputerName)

Put those two lines just after the "strOU = replace(objOU.Name,"OU=","")" line

That will let you know what the program thinks your OU and Computer name are.  Match the case in the select and if statements and that part should work.

The printUI command failed because I didn't copy your whole server name when I made the line.  Use this instead:
\\NHHDS3.NHHDSDC3.LOCAL\Upstairs_HP

as in:
rundll32 printui.dll,PrintUIEntry /in /n\\NHHDS3.NHHDSDC3.LOCAL\Upstairs_HP


We'll get this knocked out!!




 
0
 
mlsillsAuthor Commented:
Sorry for the delay getting back - one foot of snow on Monday and then problems which the users considered more important - life would be easier if they woukd just leave me alone!.

I ran the printUI command and it worked on a Win 2000 computer but not on an XP computer.

Today (Friday) I will add the lines you suggested into the script and see what results.

Thanks for your continued help!

Mark
0
 
mlsillsAuthor Commented:
I added the two lines and tested it and it returned the following:

OU= computerpolicy

ComputerName = LabComputer  (this is the correct name of the computer I tested on.

I'm not sure that the OU is returning the correct value.

Mark
0
 
dm7941Commented:
Very useful, the commands are set to get the first OU from the computer instead of the user.  

Here's how to get the OU of the user.  There is a bit of array work in here, so you will need to adjust for the correct value.  Use this test script to find out which return value you need.  I'm sure there is a more elegent way to do this, but this does work.

Set objAdsSystemInfo = CreateObject("adsysteminfo")
set objUser=GetObject("LDAP://" & objAdsSystemInfo.UserName)
strOUPath=objUser.Parent
strOU=replace(stroupath,"LDAP://","")
aOUs=split(strou,",")
msgbox aOUs(0) & "=0"
msgbox aOUs(1) & "=1"
msgbox aOUs(2) & "=2"
msgbox aOUs(3) & "=3"


this script gets the full OU name, splits it into an array and then messages the array values back to you one at a time.    Once you figure out which value it is, replace this line in the original code:

Set objOU = GetObject(objComputerName.Parent)
with
set objUser=GetObject("LDAP://" & objAdsSystemInfo.UserName)
strOUPath=objUser.Parent
strFullOU=replace(stroupath,"LDAP://","")
aOUs=split(strou,",")

then replace line
strOU = replace(objOU.Name,"OU=","")

with
strOU=aOUs(x)

where x is the correct return value from the test script.

as for the printUI command, i've actually never used it on anything but XP machines, so it still must be something in group policy that is causing it to fail.  Were you able to change those policy things for testing?

0
 
mlsillsAuthor Commented:
dm7941,

I think I understand what you are doing with this script. I tested it on my workstation and did get four different values (as I recall - I'm at home now) one was "business" which was the correct group to which I was logged in as.

I also understand the lines you want me to replace. However, then I am lost!

Which of the values do I enter in place of the "x"?

Sorry to be a pain, but I'm just not following at this point.

Mark
0
 
mlsillsAuthor Commented:
More information:

The test script returned the following:

Logged in as a student

OU=students=0
DC=nhhdsdc3=1
DC=local=2
subscript out of range '[Number: 3]'
-------------------------------------------------------------
Logged in as a teacher

OU=teacher=0
DC=nhhdsdc3=1
DC=local=2
subscript out of range '[Number: 3]'
------------------------------------------------------------
Logged in as business

OU=business=0
DC=nhhdsdc3=1
DC=local=2
subscript out of range '[Number: 3]'
-------------------------------------------------------------

In all cases the number 3 was reported as an error.

Mark
0
 
dm7941Commented:
You'll use the "0" value (0 in place of x).  The others don't matter.  The only reason to do it this way is in case you had nested OUs.

0
 
mlsillsAuthor Commented:
O.K., First thing in the morning I will give it a go!

Thanks,

Mark
0
 
mlsillsAuthor Commented:
In a test run, using a script editor, I received the following error.

\\Nhhds3\teacherhome\mlsills\! MLSills\Scripts (VBS)\SetPrintersByLocation.vbs(24,1)
  Microsoft VBScript runtime error: Variable is undefined: 'objUser'


The Script As It Stands Now:
======================
Option Explicit
Dim strOU, objNetwork, strUNCPrinter, strDefPrinter, objAdsSystemInfo, objComputerName, objOU, strUpstairsPrinter, strComputerRoomPrinter
strUpstairsPrinter="\\NHHDS3.NHHDSDC3.LOCAL\Upstairs_HP"
strComputerRoomPrinter="\\NHHDS3.NHHDSDC3.LOCAL\ComputerRoom_HP"

Set objNetwork = CreateObject("WScript.Network")

'add all printers to computer
objNetwork.AddWindowsPrinterConnection strUpstairsPrinter
objNetwork.AddWindowsPrinterConnection strComputerRoomPrinter

'pick which to set as default
Set objAdsSystemInfo = CreateObject("adsysteminfo")
Set objComputerName = GetObject("LDAP://" & objAdsSystemInfo.ComputerName)
Set objUser=GetObject("LDAP://" & objAdsSystemInfo.UserName)
strOUPath=objUser.Parent
strFullOU=replace(stroupath,"LDAP://","")
aOUs=split(strou,",")
strOU=aOUs(0)

Select Case strOU
 Case "Students"
      if left(objNetwork.ComputerName,5)="Grade" then
            objNetwork.SetDefaultPrinter strUpstairsPrinter
      else
            objNetwork.SetDefaultPrinter strComputerRoomPrinter
      end if      
 Case "Teachers"
      if left(objNetwork.ComputerName,5)="Grade" then
            objNetwork.SetDefaultPrinter strUpstairsPrinter
      else
            objNetwork.SetDefaultPrinter strComputerRoomPrinter
      end if      
 Case "Business"
End Select

WScript.Quit

========================

Mark
0
 
mlsillsAuthor Commented:
The variable definition errors ceased after adding the following to the "Dim" statement

objUser, strOUPath, strFullOU, aOUs

However, upon testiing it receive a  "subscript out of range '[numbver: 0]' " error.

Mark
0
 
dm7941Commented:
aOUs=split(strou,",")

should be
aOUs=split(strfullou,",")
0

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.

  • 14
  • 7
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now