We help IT Professionals succeed at work.

VB script to replace mapped printers

j_tech36
j_tech36 asked
on
I need help with a VB script that can do the following:

Check to see if a computer has a particular printer mapped, if it does then to delete it and map a new printer

i.e.

if exists \\server1\printer1
then
delete printer 1
and map \\server2\printer1
else nothing (if the printer doesnt exist on the computer then simply do nothing)
Comment
Watch Question

Top Expert 2005

Commented:
No problem.

I have a script that does just that.

As long as the printer share is identical and only the server needs to change, then if you provide the old name(s) and new name(s), then I can modify if for you before I post.

Author

Commented:
That would be great, can you post the script? The share names are exactly the same

Author

Commented:
Old server name
Vicky
New server name
Lamda

I assume though that this is a fairly simple vbscript that i can modify as well?
Top Expert 2005

Commented:
It should be, yes.

Name the script below "printers.vbs"

It was written to be run as part of a logon script and run only once then copy itself to the local C: drive as a flag file so it can determine if it was run before.

I have modified it for your server names, however if you have different servernames that are unequal in length then this script will fail for names longer than 5 characters.

Let me know how it works for you.

dim oFSO 

Set oFSO = CreateObject("Scripting.FileSystemObject")

 if (oFSO.FileExists ("c:\print2Z.flg")) then
	wscript.quit 
  else
	oFSO.CopyFile "printers.vbs", "c:\print2z.flg"
 end if


Set WshNetwork = CreateObject("WScript.Network")
Set oPrinters = WshNetwork.EnumPrinterConnections

dim strOldServer, strNewServer, strPrinterName, strOldDefault, strNewDefault

strComputer = "."
 Set objWMIServ = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
 Set colItems = objWMIServ.ExecQuery("Select * from Win32_Printer",,48)
  For Each objItem in colItems
    if (objItem.Attributes AND 4) = 4 then
    strOldDefault = objItem.Caption
    end if
  Next

strNewDefault = MID(strOldDefault, 8, Len(strOldDefault))
wscript.echo "Default is: " & strNewDefault

For j = 0 to oPrinters.Count - 1 Step 2
  
 if Left(oPrinters.Item(j+1), 2) = "\\" then


   strOldServer = Left(oPrinters.Item(j+1), 7)

   
   Select Case strOldServer
   	Case "\\Vicky"
   		strNewServer = "\\Lamda"

   	Case Else
   		strNewServer = strOldServer
   	End Select
   	
   strPrinterName = MID(oPrinters.Item(j+1), 8, Len(oPrinters.Item(j+1)))


   Wscript.Echo strNewServer & strPrinterName
   
              
   WshNetwork.AddWindowsPrinterConnection strNewServer & strPrinterName
   WshNetwork.RemovePrinterConnection strOldServer & strPrinterName
   
    if strPrinterName = strNewDefault then 
      WshNetwork.SetDefaultPrinter strNewServer & strNewDefault  
    end if

 end if

Next


wscript.echo "Script Complete...."

Open in new window

Author

Commented:
Ok, this looks great. Only issue is I gave you the wrong server name the new server name is

Lambda

Which is longer than 6 characters. what do we need to modify in your script to make this work now?
Top Expert 2005

Commented:
Correct line 39 and run it on a test machine from a cmd window: cscript printers.vbs

There are output lines throughout the script to ensure you are seeing the correct info.

Line 48 would output what the servername\printername should be changed to - just make sure it looks right.

If you run it more than once to fine tune it, make sure to delete the C:\print2z.flg file on the local machine so it runs again.

Author

Commented:
I'm clear on line 39 & 48 - I just wasnt clear on why you said it wouldnt work if the servername was over 5 characters. Can you elaborate on that? I will try to get it tested as soon as possible.
Top Expert 2005

Commented:
Only the original servernames matter.  The script is coded for a fixed length name (including the leading \\).  If you wanted to change other servers to the new one, then those names need to be the same length or we'll need to add some lines to get the length rather than hard-coding it.

I used this script to check for 5 different servernames (in the Case section of the script) and, depending on the originating servername, I would assign it to a new servername.  We changed 5 print servers out and consolidated those into 3 new servers.  Our original servers had a fixed length naming convention and our new servers had a longer fixed length naming convention - so effectively the source servers were an exact length and the destination servers were also an exact length.  It's a little simpler when you have a fixed length with respect to coding the script instead of having to dynamically determine the servername length and use a variable.

Author

Commented:
So to be clear if I use your script as is and have the original server name as

\\vicky
and the new server name as
\\lambda

will it work OK even though they are different character lengths?
Top Expert 2005

Commented:
It should, yes.
Top Expert 2005

Commented:
Hey Lee,

I think this should work, however without feedback it's hard to tell if it did what the OP wanted.

NM

Author

Commented:
In testing today. Will give you an update

Author

Commented:
So what it seems to do is remove all the printers from the computer, it does not change them over. Can i get a little more assistance with this please?
Top Expert 2005

Commented:
Yes, no problem.

I'll need the original print server mapping and the new one.

I'll be taking my son to basketball shortly, but I'll look after this when I return.

A screenshot of a workstation's printer screen might be helpful too.

Author

Commented:
Here is a screen shot of the w/s though i dont think it will tell you much, i think more importantly i did a prnmngr listing so you could see the names of the old and the new pritners

Old Server: Vicky
Printer name \\vicky\Business Office - 01 - HP LaserJet 2300 - PCL
Share name Business Office - 01 - HP 2300

Printer name \\vicky\Business Office - 02 - HP Color LaserJet 3600n
Share name Business Office - 02 - HP 3600n

Printer name \\vicky\Canon Copier - 1st Floor Administration - PCL
Share name Canon Copier - 1st Floor Administration - PCL

Printer name \\vicky\Canon Copier - 1st Floor Student Store - PS
Share name Canon Copier - Student Store - PS


New Server: Lambda

Printer name \\lambda\Business Office - 01 - HP LaserJet 2300 - PCL
Share name Business Office - 01 - HP 2300


Printer name \\lambda\Business Office - 02 - HP Color LaserJet 3600n
Share name Business Office - 02 - HP 3600n


Printer name \\lambda\Canon Copier - 1st Floor Administration - PCL
Share name Canon Copier - 1st Floor Administration - PCL

Printer name \\lambda\Canon Copier - 1st Floor Student Store - PS
Share name Canon Copier - Student Store - PS

>> This is just a few of the over 40 printers we have. I'm also attaching the txt file to make it easier to see. Printersample.txt screen shot
Top Expert 2005

Commented:
Ok, those are good.

A print screen from one of the workstations showing the printers folder would also be helpful.

I'll take a look at the code now.

Top Expert 2005
Commented:
What happens with this script?


dim oFSO 

Set oFSO = CreateObject("Scripting.FileSystemObject")

 if (oFSO.FileExists ("c:\print2Z.flg")) then
	wscript.quit 
  else
	oFSO.CopyFile "printers.vbs", "c:\print2z.flg"
 end if


Set WshNetwork = CreateObject("WScript.Network")
Set oPrinters = WshNetwork.EnumPrinterConnections

dim strOldServer, strNewServer, strPrinterName, strOldDefault, strNewDefault

strComputer = "."
 Set objWMIServ = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
 Set colItems = objWMIServ.ExecQuery("Select * from Win32_Printer",,48)
  For Each objItem in colItems
    if (objItem.Attributes AND 4) = 4 then
    strOldDefault = objItem.Caption
    end if
  Next

strNewDefault = MID(strOldDefault, 8, Len(strOldDefault))
wscript.echo "Default is: " & strNewDefault

For j = 0 to oPrinters.Count - 1 Step 2
  
 if Left(oPrinters.Item(j+1), 2) = "\\" then


   strOldServer = Left(oPrinters.Item(j+1), 7)

   
   Select Case strOldServer
   	Case "\\vicky"
   		strNewServer = "\\lambda"

   	Case Else
   		strNewServer = strOldServer
   	End Select
   	
   strPrinterName = MID(oPrinters.Item(j+1), 8, Len(oPrinters.Item(j+1)))


   Wscript.Echo strNewServer & strPrinterName
   
              
   WshNetwork.AddWindowsPrinterConnection strNewServer & strPrinterName
   WshNetwork.RemovePrinterConnection strOldServer & strPrinterName
   
    if strPrinterName = strNewDefault then 
      WshNetwork.SetDefaultPrinter strNewServer & strNewDefault  
    end if

 end if

Next


wscript.echo "Script Complete...."

Open in new window

Author

Commented:
That worked much better.

2 questions.
1) is there a way to remove the pop-up window that requires an OK click for each printer?

2) if the script is run twice it actually removes printers that it already converted - is there a way to stop this behavior?
Top Expert 2005
Commented:
1) Sure, remove line 27 & 48.

2) Reverse lines 51 and 52 so that it removes first, then adds next.


LeeTutorretired
Top Expert 2009

Commented:
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.

Author

Commented:
Everything worked perfectly - Got too busy to come back until now. Thank you!
Top Expert 2005

Commented:
Anytime!