Link to home
Start Free TrialLog in
Avatar of dmcoop
dmcoopFlag for United States of America

asked on

Scripting Print Server Printers to Domain Computers

I have been studying scripting printers on my domain for some time now.  I believe I am more confused than ever too.  While I have seen many solutions/suggestions on scripting and some of the trials I have run have been successful (in part) . . . I have been unable to find/develop a solution to my exact needs.

-------------------------------------------------

Here is the way I am currently configured:

- I run only Windows 2003 Standard servers.  My print server has 12 network printers installed on it that are spread around two different buildings and multiple areas in those buildings.

- All PCs are XP Pro.

- We do not use roaming profiles.  Users only logon to their own computer.

- Some users have a local printer on their computer and these are set as their default printer.  These are normally connected to the USB port, but a couple are still on LPT1.  The users with local printers also print to the network printers in their area when needed.

- Some users only print to the network printers.  We set the one in their area as the default.

- Most users have PDF printers installed and fax printers too.

- Presently myself and the other network admin manually add/remove all printers from each users computer as necessary.

-----------------------------------------------------------------------------

Here is where I want to be (and need help/advice on getting here):

- I want to continue using my print server for all network printers.

- All PCs will remain XP Pro.

- We do not want to use roaming profiles.

- For the users that have PDF printers and fax printers . . . the scripting must leave those alone.  The scripting must also leave alone the local printer and let it remain as the default.

- I want all network printers to be added (and old ones deleted) by script from the users PCs when they logon.

- Users that have local printers need to keep those print drivers installed and their local printer as the default printer.

- Users that have local printers installed need to have the network printers mapped to their PC each time they logon, but not change the default printer away from their local printer (have I said that enough ;-) )

- Users that print only to network printers need to have the printers installed each time they logon and the network printer that is in their area set as the default printer.

- For all users . . . I need old network printers that no longer exist to be removed from their PC.

- I want to do all of this through scripting and never have to touch a workstation again to add/remove a network printer.

SO HERE IS MY QUESTION:

How do I get to where I want to be using only the scripting abilities of Windows Server 2003 Std and XP Pro?  I don’t want to purchase a 3rd party software unless absolutely necessary.

Thanks all for your help.
Avatar of Mazaraat
Mazaraat
Flag of United States of America image

I can get you 90% of the way =), the removal of the old printers gets a little tricky, but can be accomplished also.  I will cut and paste a part of my logon script that maps network drives from addprinters.csv file and lets the users select their default printer, then another piece that removes printers based on entries in a removeprinters.csv.  Does this sound like what your looking for?


ASKER CERTIFIED SOLUTION
Avatar of Mazaraat
Mazaraat
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You can assign the script in ADUC or through group policies (user policy) on an OU that contains the users
Avatar of rchein
rchein

A slight variation on this would be instead of having the user have to select the default printer, use security groups to control what printer gets set as default.

I have one group that does not change default printer.
I then have a group for every other printer.  If the user is a member of the group, then I set the default to the printer for this group.

I use the examples from http://www.rlmueller.net/products.htm  for figuring out group membership.

Avatar of dmcoop

ASKER

Ok.  Thanks to both of you for posting.

Let me give this a shot and I'll post back.

One question Mazaraat . . . are you saying the script files should be saved as a .csv file type (vs. a .vbs)?

Thanks again.
Avatar of dmcoop

ASKER

Mazaraat - I just re-read what you posted.  Nevermind on the .csv file type question.  Although it does wonder me why .csv?
Avatar of dmcoop

ASKER

All right Mazaraat - this looks like it will work.  I'm about to lab it right now.  I do have one more question (which the lab may answer) . . . Does the default printer selection box appear each time a user logs on or just when there are changes?  Also, does it effect local printers that are set to the default printer?
Its been a long time, I don't remember why I was using csv...though it could just as well be a txt file...



Good Luck, let us know if you have any issues
Each time it runs....you can comment out the selection after the first time it runs...I use the script when setting up networks or migrations...=)

IT will look at ALL printers including local, so they can choose (or you)
Avatar of dmcoop

ASKER

Mazaraat -

The Remove Printers sections seems to work just fine.  However I keep getting an error out of the Add Printers section.

I broke it down to just run as an Add Printers only script.  Here is it:

'******************************************************************************
'******************************************************************************
'**          Printer mapping
'******************************************************************************
'******************************************************************************
Dim objGroupList, objUser, objShell, strGroup, objNet, strNTName
Dim strNetBIOSDomain, strHomeDrive, strHomeShare, CurrentUser, aPrinter, x
Dim PrnList, fso, WshShell, WshNetwork, PrinterPath, LogonPath, strGroup1
Dim strUserName, Return

Set objNet = CreateObject("WScript.Network")
Set fso = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("WScript.Shell")
LogonPath = fso.GetParentFolderName(WScript.ScriptFullName)

call Mapprinters

'*************
'add printers
'*************

sub Mapprinters()
If fso.FileExists(LogonPath & "\Printers_add.csv") Then
  Set prnlist = fso.OpenTextFile(logonpath & "\Printers_add.csv", fsoForReading)
  aPrinter = Split(prnlist.Readall,vbcrlf)
For x = 0 to UBound(aPrinter)
  PrinterPath = aPrinter(x)
  objNet.AddWindowsPrinterConnection PrinterPath
Next
  'Wscript.Echo "Done. Mapped " & x & " Printers"

End If
End Sub
Set objGroupList = Nothing
Set objUser = Nothing
Set objNet = Nothing      
Set fso = nothing
Set objShell = nothing
'msgbox "Logon Script Completed"

--------------------------------------------------------

I get the following error when this script runs:

Window Title: Windows Script Host

Script: C:\Scripts\NetworkPrinterMappingOnly.vbs
Line: 24
Char: 3
Error: Invalid procedure call or argument
Code: 800A0005
Source: Microsoft VBSript runtime error

If I am readying this error correctly it is referencing this line:

  Set prnlist = fso.OpenTextFile(logonpath & "\Printers_add.csv", fsoForReading)

Any ideas???  The Printers_add.csv file is in the same folder as the script.

your missing:

const fsoForReading = 1
Avatar of dmcoop

ASKER

Should the line read:

Set prnlist = fso.OpenTextFile(logonpath & "\Printers_add.csv", const fsoForReading=1)

I added "const" in front of "fsoForReading" and also add the "=1" after.

I have it that way now and I get the same error but Char: 67 instead of 3.
Avatar of Bradley Fox
No, at the top of your script after your dim statements add on it's own line


Const fsoForReading = 1
alternatly you can just change

Set prnlist = fso.OpenTextFile(logonpath & "\Printers_add.csv", fsoForReading)

to

Set prnlist = fso.OpenTextFile(logonpath & "\Printers_add.csv", 1)
Yea, I use that constant in a few other places in my logon script, so you could set it like above or add the line just below the dim statements....if your going to use the argument in more than one area use the const
const fsoForReading = 1    





'******************************************************************************
'******************************************************************************
'**          Printer mapping
'******************************************************************************
'******************************************************************************
Dim objGroupList, objUser, objShell, strGroup, objNet, strNTName
Dim strNetBIOSDomain, strHomeDrive, strHomeShare, CurrentUser, aPrinter, x
Dim PrnList, fso, WshShell, WshNetwork, PrinterPath, LogonPath, strGroup1
Dim strUserName, Return
const fsoForReading = 1

Set objNet = CreateObject("WScript.Network")
Set fso = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("WScript.Shell")
LogonPath = fso.GetParentFolderName(WScript.ScriptFullName)

call Mapprinters

'*************
'add printers
'*************

sub Mapprinters()
If fso.FileExists(LogonPath & "\Printers_add.csv") Then
  Set prnlist = fso.OpenTextFile(logonpath & "\Printers_add.csv", fsoForReading)
  aPrinter = Split(prnlist.Readall,vbcrlf)
For x = 0 to UBound(aPrinter)
  PrinterPath = aPrinter(x)
  objNet.AddWindowsPrinterConnection PrinterPath
Next
  'Wscript.Echo "Done. Mapped " & x & " Printers"

End If
End Sub
Set objGroupList = Nothing
Set objUser = Nothing
Set objNet = Nothing      
Set fso = nothing
Set objShell = nothing
'msgbox "Logon Script Completed"

--------------------------------------------------------
Avatar of dmcoop

ASKER

doh . . .

Ok I see that now.  Thanks.

I just put the line in and it added the printers.

Still testing.  I'll post back as soon as I get it working or hit my next roadblock.

Thanks,

Dean
No problem =) I can't found the times a script didnt work due to my misspelling the simple things =)
Avatar of dmcoop

ASKER

Ok.  More progress.  I put the script back together and everything works great as long as all the printers that are in the "Printers_remove.csv" file are actually on the users computer.

How do I get the Remove Printers portion of the script to move on without erroring out when it tries to remove a printer that the user does not have?  Basically I want to list all the printers that are on my print server now to be removed.  We are rolling out a new print server with a new name and those are the ones I want to add back in.

The error now is;

Error: This network connection does not exist
Code: 800708CA
Source: WSHNetwork.RemoveNetworkDrive
add a line above the dim statements

on error resume next
Avatar of dmcoop

ASKER

Perfect.

Now I just have to decide ADUC or group policies.  I'm leaning towards group policies and will try that first.

I'll post back in a little while to let you know how this goes.

Thank you for the help and advice.
np, glad to help =)
Avatar of dmcoop

ASKER

Mazaraat - There seems to be a bug in the Remove Printers portion of the script.  Hopefully you can help.

If a user has installed on their PC:

\\oldprintserver\printer1
\\oldprintserver\printer4
\\oldprintserver\printer5

And does not have printers 2 & 3 the script removes printer1 then leaves 4 & 5 and continues on adding the new printers from the new server.  The end result is that only up to a certain point do the old printers get removed.

The "Printers_remove.csv" file looks like this:

\\oldprintserver\printer1
\\oldprintserver\printer2
\\oldprintserver\printer3
\\oldprintserver\printer4
\\oldprintserver\printer5

And the Remove Printers portion of the script looks like this:

'************
'Remove Printers
'************
sub Delprinters()
Dim errorObject
'Set errorObject = CreateObject("Wscript.Error") <-> I disabled this for another issue.
If fso.FileExists(LogonPath & "\Printers_remove.csv") Then
  Set prnlist = fso.OpenTextFile(logonpath & "\Printers_remove.csv", fsoForReading)
  aPrinter = Split(prnlist.Readall,vbcrlf)
For x = 0 to UBound(aPrinter)
  PrinterPath = aPrinter(x)
  objNet.RemovePrinterConnection PrinterPath, true
  'DisplayErrorInfo
Next
  'Wscript.Echo "Done. Removed " & x & " Printers"
end if
End Sub
Avatar of dmcoop

ASKER

I think I have a work around for this last issue of the script not skipping onto the next printer.  I can create a script that I run once and first to map ALL 12 of the printers that are on my old print server.  Then when the script above runs it will remove them all and then create all new printers mapped to the new print server.

I'll let you know how it goes.
Avatar of dmcoop

ASKER

I am awarding points to Mazarat since his first answer was correct - I am about 90% of the way now and what he posted worked after some tweaking.

I need the other 10% though.  Below is a link to a new question I have posted about this in case anyone wants to help out.  I don't understand group policy well enough to know what is blocking me now so I have open another question.

https://www.experts-exchange.com/questions/21847647/Problems-Getting-New-Logon-Script-New-GPO-to-Work.html

Thanks to all for posting.