?
Solved

Problem with VBS script to re-map Printers to new Print Server

Posted on 2009-05-01
13
Medium Priority
?
1,298 Views
Last Modified: 2012-05-06
Hi guys.  We are getting a new print server, and I am trying to use a vbs script to remap everyone to use the new print server.  This is very easy, but I'm trying to avoid adding printers for people who do not currently have the printers mapped.  Out of the 10 or so printers we have, some people only use a couple.  Here's the script, which works great, but adds all of them:
'****************************************************************
ON ERROR RESUME NEXT

Dim Printers

Set Printers = CreateObject("WScript.Network")

'**********REMOVES PRINTER ON PRINTSVR1, THEN ADDS PRINTER ON PRINTSVR2**********

Printers.RemovePrinterConnection "\\PRINTSVR1\PRINTER-1"
Printers.AddWindowsPrinterConnection "\\PRINTSVR2\PRINTER-1"

Printers.RemovePrinterConnection "\\PRINTSVR1\PRINTER-2"
Printers.AddWindowsPrinterConnection "\\PRINTSVR2\PRINTER-2"

Printers.RemovePrinterConnection "\\PRINTSVR1\PRINTER-3"
Printers.AddWindowsPrinterConnection "\\PRINTSVR2\PRINTER-3"

Printers.RemovePrinterConnection "\\PRINTSVR1\PRINTER-4"
Printers.AddWindowsPrinterConnection "\\PRINTSVR2\PRINTER-4"

Set Printers = Nothing
'****************************************************************


I'm assuming I need some sort of IF EXIST, THEN do this, IF NOT EXIST, then skip it and do that...

Something like this, which doesn't work:

'****************************************************************
ON ERROR RESUME NEXT

Dim Printers

Set Printers = CreateObject("WScript.Network")

'**********If Printer Exists, remove it from PRINTSVR1, then add it on PRINTSVR2**********

If exist "\\PRINTSVR1\PRINTER-1" Then PRINTER1 Else PRINTER2

:PRINTER1
Printers.RemovePrinterConnection "\\PRINTSVR1\PRINTER-1"
Printers.AddWindowsPrinterConnection "\\PRINTSVR2\PRINTER-1"

If exist "\\PRINTSVR1\PRINTER-2" Then PRINTER2 Else PRINTER3

:PRINTER2
Printers.RemovePrinterConnection "\\PRINTSVR1\PRINTER-2"
Printers.AddWindowsPrinterConnection "\\PRINTSVR2\PRINTER-2"

If exist"\\PRINTSVR1\PRINTER-3" Then PRINTER3 Else END

:PRINTER3
Printers.RemovePrinterConnection "\\PRINTSVR1\PRINTER-3"
Printers.AddWindowsPrinterConnection "\\PRINTSVR2\PRINTER-3"

:END

Set Printers = Nothing
'****************************************************************


I would really like to use the existing script if possible, but need some sort of syntax change to make it work.  I'm not a programmer by any stretch of the imagination.  As a big bonus, I would love to be able to keep the default printer for whatever people are currently using if possible.

Thank you for any and all assistance you can provide.
0
Comment
Question by:redmanjb
  • 7
  • 5
13 Comments
 
LVL 7

Expert Comment

by:sfarazmand
ID: 24280255
Try this link, there is a link to download a script which will do just what you want.  You'll need to change the server names in the script with your own.

http://www.sysadmintales.com/2008/07/25/print-server-migration-script/
0
 

Author Comment

by:redmanjb
ID: 24281172
Awesome, thank you very much for the quick reply sfarazmand.  Unfortunately, the script didn't seem to do anything.  I changed the "NewPrintServerName" to the name of our new print server ( from "SERVER05" to "PRINTSVR2"), and changedthe "If ServerName" from "SERVER01" to "PRINTSVR1", and saved it with a "vbs" extension.  Do I need to change anything else?
Thanks again! :)
0
 
LVL 7

Expert Comment

by:sfarazmand
ID: 24281560
You'll want to remove the else if statements since you only have once server.  Then set it as a startup script through group policy. It should work at that point. I'm not an expert at scripting but what is below should do the trick.
'On Error Resume Next 
strComputer = "." 
NewPrintServerName = "\\PRINTSVR2\"
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") 
Set colItems = objWMIService.ExecQuery("Select * from Win32_Printer",,48) 
For Each objItem in colItems 
 
	' Get some values
	DefaultPrinter = objItem.Default 
	PrinterQueue = objItem.DeviceID
	ServerName = objItem.ServerName
	PrinterName = objItem.ShareName
	
	' Find out if the printer object is a network mapped printer from print server
	'  and not from a PC (we only want to remap printers on the print server)
	
	If ServerName = "\\PRINTSVR1" Then
	
		' Remove mapping On old print server
		Set objNetworkPrinterDelete = WScript.CreateObject("WScript.Network")
		objNetworkPrinterDelete.RemovePrinterConnection PrinterQueue
		' Add mapping to new print server
		NewPrintMapping = NewPrintServerName & PrinterName
		Set objNetworkPrinterAdd = CreateObject("WScript.Network")
		objNetworkPrinterAdd.AddWindowsPrinterConnection NewPrintMapping
		' Set default printer if it previously was the default printer
		If DefaultPrinter = "True" Then
			Set objNetworkPrinterMakeDefault = CreateObject("WScript.Network")
			objNetworkPrinterMakeDefault.SetDefaultPrinter NewPrintMapping
		End If
				
	End If
	
Next 
 
WScript.Quit

Open in new window

redmanjb.zip
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:redmanjb
ID: 24282194
Hmmm...still not doing anything.  I should be able to just run it manually right?  We won't be using it as a startup script, we have another program that we use where we can run scripts remotely on any/all machines.  It just seems like this should run when double-clicking...but nothing happens.
0
 
LVL 7

Expert Comment

by:sfarazmand
ID: 24282778
You should be able to run it manually. If you're running it manually, I have a personal script I created to delete current printers and prompt you to select a new printer from a list of printers on the new print server using con2prt.

The basics are:

CON2PRT /f  => deletes current printers
CON2PRT /c \\PrintServer\PrintShare  => connects to printer PrintShare
CON2PRT /cd \\PrintServer\PrintShare  => connects to and set PrintShare as default.
Net View \\printserver  =>Lists Printers on the PrintShare.

I forgot how I had it set up to delete printers, then list printers and prompt you to enter a printer form the list.
0
 

Author Comment

by:redmanjb
ID: 24283212
I really want this to be completely transparent to the users, as some of them are not particularly technical, which is why I wanted to use a vbs.  Do you have any idea what my initial script is missing, how to basically correct it so that it would do the "IF EXIST, THEN do this, IF NOT EXIST, then skip it" thing?  
That other script (which for some reason will not run for me) would be great, but I would like the ability to specify the shared printers, as I would like the opportunity to change the printer share names of a few of them so that they would match our naming convention.  But, if I can get that script you linked to to work, that would be fine for now.  
The really important thing is to keep everything the same from the end user's perspective, but still change the print server their printers are connected to.
0
 
LVL 7

Expert Comment

by:sfarazmand
ID: 24283551
I looked at the script and it seems like it should be working. Make sure the test machine has the old printers, is logged in by an admin. Ping the print server to be sure it can see it. run the script.

I also found the attached script which can map printers to OUs. Not sure how you're set up.

I'm not a script expert so I can't troubleshoot anything more than I have without a test which I don't have the resources to do at the moment.
'Option Explicit
'On Error Resume Next
 
'=====================================================================
'
'
' NAME: @****** printer map
'
' Author: Jerrid
' DATE  : 8/3/2004
'
' COMMENT: <This script is to map specific printers to specific 
' computer OU's. 
' It deletes all existing network printers and then installs the 
' desired network 
' printer(s) and sets the default.  Local Printers are not deleted.
'====================================================================
 
Set oNetwork = WScript.CreateObject("WScript.Network")
 
'Remove only network printers
 
Set oOldPrinters = oNetwork.EnumPrinterConnections
For i = 0 to oOldPrinters.Count - 1
If mid(oOldPrinters.Item(i), 1, 2) = "\\" Then
sOldPrinterPath = oOldPrinters.Item(i)
oNetwork.RemovePrinterConnection sOldPrinterPath, True, True
End If
Next
 
'This script adds the @off printer and sets as default
'You Only need the SetDefaultPrinter option if you want more than 1 
'printer to 'connect, otherwise it can be commented out
 
	oNetwork.AddWindowsPrinterConnection"\\(server)\printer)"
 
        oNetwork.SetDefaultPrinter"\\(server)\(printer)"

Open in new window

0
 

Author Comment

by:redmanjb
ID: 24300048
This is not a communication issue or anything.  As I mentioned in the initial posting, the following works perfectly fine:
Printers.RemovePrinterConnection "\\PRINTSVR1\PRINTER-1"
Printers.AddWindowsPrinterConnection "\\PRINTSVR2\PRINTER-1"

This shows that the connections with the print servers are fine.  

As far as the additional script you have posted, I'm confused about the statement you made about mapping "printers to OUs", which is not possible.  I'm assuming you mean that you can create a GPO that assigns a logon script that includes printer mappings, and link that GPO to a particular OU...but that is not what I need.

The script you provided does not accomplish the task of removing the currently installed printers for each user, and installing ONLY the printers that the person previously had installed, which are shared on the NEW print server.  With your script, it will remove all, and reinstall all...which my initial script already does.

I really think that the script I had proposed can easily be made to work, with only some syntax modification dealing with the IF, THEN, ELSE.

Any ideas?  I really appreciate your input sfaraznand,

Again, here's my proposed script:


 
'****************************************************************
ON ERROR RESUME NEXT
 
Dim Printers
 
Set Printers = CreateObject("WScript.Network")
 
'**********If Printer Exists, remove it from PRINTSVR1, then add it on PRINTSVR2**********
 
If exist "\\PRINTSVR1\PRINTER-1" Then PRINTER1 Else PRINTER2
 
:PRINTER1
Printers.RemovePrinterConnection "\\PRINTSVR1\PRINTER-1"
Printers.AddWindowsPrinterConnection "\\PRINTSVR2\PRINTER-1"
 
If exist "\\PRINTSVR1\PRINTER-2" Then PRINTER2 Else PRINTER3
 
:PRINTER2
Printers.RemovePrinterConnection "\\PRINTSVR1\PRINTER-2"
Printers.AddWindowsPrinterConnection "\\PRINTSVR2\PRINTER-2"
 
If exist"\\PRINTSVR1\PRINTER-3" Then PRINTER3 Else END
 
:PRINTER3
Printers.RemovePrinterConnection "\\PRINTSVR1\PRINTER-3"
Printers.AddWindowsPrinterConnection "\\PRINTSVR2\PRINTER-3"
 
:END
 
Set Printers = Nothing
'****************************************************************

Open in new window

0
 
LVL 7

Expert Comment

by:sfarazmand
ID: 24304181
Yes you got the gist of linking to OUs. The original one I sent you should work because it checks for the current printer what server it's connected to and adds the same printer with the new print server. Our script guru is in today. Let me see if I can get him to check it and run some tests.
0
 
LVL 7

Accepted Solution

by:
sfarazmand earned 2000 total points
ID: 24328026
redmanjb,

Since you do not want to use group policy settings linked to OUs to resolve this, you have to either run this on each machine or set it as a startup/shutdown script. Your code will have to check for each printer. Also that first portion of you If/Then/Else states: "If exist "\\PRINTSVR1\PRINTER-1" Then PRINTER1 Else PRINTER2" does not go to the next If statement, it jumps to the action.

The following code works to remove printers currently mapped and to remap then to the new printserver (not all printers, just what is currently there).

I tested the whole code as well as simply breaking it down to delete and add seperately. I ran it on a workstation I had admin rights to.

If it's not working for you there, I'm not sure what to say.
'On Error Resume Next 
strComputer = "." 
NewPrintServerName = "\\NewServer\"
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") 
Set colItems = objWMIService.ExecQuery("Select * from Win32_Printer",,48) 
For Each objItem in colItems 
 
	' Get some values
	DefaultPrinter = objItem.Default 
	PrinterQueue = objItem.DeviceID
	ServerName = objItem.ServerName
	PrinterName = objItem.ShareName
	
	' Find out if the printer object is a network mapped printer from print server
	'  and not from a PC (we only want to remap printers on the print server)
	
	If ServerName = "\\OldServer" Then
	
		' Remove mapping On old print server
		Set objNetworkPrinterDelete = WScript.CreateObject("WScript.Network")
		objNetworkPrinterDelete.RemovePrinterConnection PrinterQueue
		' Add mapping to new print server
		NewPrintMapping = NewPrintServerName & PrinterName
		Set objNetworkPrinterAdd = CreateObject("WScript.Network")
		objNetworkPrinterAdd.AddWindowsPrinterConnection NewPrintMapping
		' Set default printer if it previously was the default printer
		If DefaultPrinter = "True" Then
			Set objNetworkPrinterMakeDefault = CreateObject("WScript.Network")
			objNetworkPrinterMakeDefault.SetDefaultPrinter NewPrintMapping
		End If
		
			
	End If
	
Next 
 
WScript.Quit

Open in new window

testprint.txt
0
 

Author Closing Comment

by:redmanjb
ID: 31576896
Hi sfarazmand.  Not really sure what the issue was or anything, but I just tried the one that you last posted, and it worked flawlessly!  Thank you so very much for sticking with it, you have really helped me out tons! :)
0
 
LVL 7

Expert Comment

by:sfarazmand
ID: 24397652
Glad i could help!
0
 

Expert Comment

by:dontpanic72
ID: 25145564
Glad you found my script useful.  It was indeed tested and working before posting for public consumption.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The article covers five tools all IT professionals should know about, as they up productivity by a great deal!
It is a real story and is one of my scariest tech experiences. Most users think that IT experts like us know how to fix all computer problems. However, if there is a time constraint and you MUST not fail the task or you will lose your job, a simple …
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses
Course of the Month14 days, 21 hours left to enroll

840 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