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

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.
redmanjbAsked:
Who is Participating?
 
sfarazmandConnect With a Mentor Commented:
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
 
sfarazmandCommented:
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
 
redmanjbAuthor Commented:
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
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
sfarazmandCommented:
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
 
redmanjbAuthor Commented:
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
 
sfarazmandCommented:
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
 
redmanjbAuthor Commented:
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
 
sfarazmandCommented:
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
 
redmanjbAuthor Commented:
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
 
sfarazmandCommented:
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
 
redmanjbAuthor Commented:
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
 
sfarazmandCommented:
Glad i could help!
0
 
dontpanic72Commented:
Glad you found my script useful.  It was indeed tested and working before posting for public consumption.
0
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.

All Courses

From novice to tech pro — start learning today.