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

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

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.
0
dmcoop
Asked:
dmcoop
  • 11
  • 10
  • 2
  • +1
1 Solution
 
MazaraatCommented:
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?


0
 
MazaraatCommented:
OK here it is, now all you have to do is make 2 text files with csv extensions called printers_add and printers_remove.  Do not have any extra spaces at the end of the lines or below the last entry.  The files should be located in the same directory as your script or change the script to where you want to put them.

entries in the printers_add.csv and printers_remove.csv should look like this when opened in notepad:

\\servername\printershare
\\servername\printershare2



script
'******************************************************************************
'******************************************************************************
'**            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 Delprinters
call Mapprinters

************
Remove Printers
************
sub Delprinters()
Dim errorObject
const fsoForReading = 1
'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

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

'******************************************************************************
'******************************************************************************
'**            Printer default selection box
'******************************************************************************
'******************************************************************************
Title = "Title"
Text = "Hi, a new server as been put in place and I need you to change your prefered printer. Listed below are the rinters currently available to you.  Please enter the number of the printer that is located on SERVERNAME. Please select a number accordingly" & vbCrLf & vbCrLf
Set oPrnEnum = objNet.EnumPrinterConnections
j = oPrnEnum.Count
For i = 0 To j - 1 Step 2
    Text = Text & (i/2) & vbTab
    Text = Text & oPrnEnum(i) & vbCrLF & oPrnEnum(i+1) & vbCrLf
    Next
tmp = InputBox(Text, "Company Default printer selector", 0)
If tmp = "" Then
    WScript.Echo "No user input, aborted"
    WScript.Quit
End If
tmp = CInt(tmp)
If (tmp < 0) Or (tmp > (j/2 - 1)) Then
    WScript.Echo "Wrong value, aborted"
    WScript.Quit
End If
printer = oPrnEnum(tmp*2 + 1)
objNet.SetDefaultPrinter printer
MsgBox "Thank you, your default printer has been successfully set to " & printer, _
        vbOKOnly + vbInformation, Title
       

End If
End Sub
Set objGroupList = Nothing
Set objUser = Nothing
Set objNet = Nothing      
Set fso = nothing
Set objShell = nothing
'msgbox "Logon Script Completed"
0
 
MazaraatCommented:
You can assign the script in ADUC or through group policies (user policy) on an OU that contains the users
0
Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

 
rcheinCommented:
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.

0
 
dmcoopAuthor Commented:
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.
0
 
dmcoopAuthor Commented:
Mazaraat - I just re-read what you posted.  Nevermind on the .csv file type question.  Although it does wonder me why .csv?
0
 
dmcoopAuthor Commented:
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?
0
 
MazaraatCommented:
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
0
 
MazaraatCommented:
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)
0
 
dmcoopAuthor Commented:
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.

0
 
MazaraatCommented:
your missing:

const fsoForReading = 1
0
 
dmcoopAuthor Commented:
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.
0
 
mcsweenSr. Network AdministratorCommented:
No, at the top of your script after your dim statements add on it's own line


Const fsoForReading = 1
0
 
mcsweenSr. Network AdministratorCommented:
alternatly you can just change

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

to

Set prnlist = fso.OpenTextFile(logonpath & "\Printers_add.csv", 1)
0
 
MazaraatCommented:
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"

--------------------------------------------------------
0
 
dmcoopAuthor Commented:
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
0
 
MazaraatCommented:
No problem =) I can't found the times a script didnt work due to my misspelling the simple things =)
0
 
dmcoopAuthor Commented:
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
0
 
MazaraatCommented:
add a line above the dim statements

on error resume next
0
 
dmcoopAuthor Commented:
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.
0
 
MazaraatCommented:
np, glad to help =)
0
 
dmcoopAuthor Commented:
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
0
 
dmcoopAuthor Commented:
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.
0
 
dmcoopAuthor Commented:
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.

http://www.experts-exchange.com/Operating_Systems/Windows_Server_2003/Q_21847647.html

Thanks to all for posting.
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.

  • 11
  • 10
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now