Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding & removing printers

Status
Not open for further replies.

58sniper

MIS
Apr 25, 2004
9,152
US
I'm new to the vbs world, but have a background in PHP. Based on the Mark's FAQ ( I've put this together, but seem to be missing something somewhere.

Ideally, what I'd like to do is cycle through the networked printers installed on a workstation. If it finds a specific printer installed, it should remove it, and install a printer from a different server. Basically, we're migrating from one server to another. So I'm essentially going to just have a loop for each printer I need to deal with (about 8). Here is what I have:

Code:
ON ERROR RESUME NEXT

Dim WSHShell, WSHNetwork, Path

Set WSHShell = CreateObject("WScript.Shell")
Set WSHNetwork = CreateObject("WScript.Network")
Set WSHPrinters = WSHNetwork.EnumPrinterConnections
For LOOP_COUNTER = 0 To WSHPrinters.Count - 1 Step 2
    If WSHPrinters.Item(LOOP_COUNTER +1) = "\\old2003\4050South" Then
      WSHNetwork.RemovePrinterConnection "\\old2003\4050South",True,True    
      WSHNetwork.AddWindowsPrinterConnection "\\new2003\4050North"  
    End If
Next

'Clean Up Memory We Used
set WSHNetwork = Nothing
set WSHSHell = Nothing
Set WSHPrinters = Nothing
  
'Quit the Script
wscript.quit

That does not pass the IF statement, so nothing happens, including no visible errors.

I'm hardcoding the printer names only because they actually change names on the new server.

In a perfect world, I'd like to detect the current default printer so that when I replace it with the printer on the new server, I can set the new default. But that's just a wish.

Ideas as to what I'm doing wrong?

Pat Richard, MCSE(2) MCSA:Messaging, CNA(2)
 
Try adding this under the For:
Wscript.Echo WSHPrinters.Item(LOOP_COUNTER +1)

So you can see what the values are that it is finding.

My recomendation for solving this however would be to simply use the scritp to remove all networked printers and then add back the specific ones you wish to map.

I hope you find this post helpful.

Regards,

Mark
 
Okay - with that addition, it shows me two printers, and lists them using the full name (not the share name). I tried putting the full name in the script, but that didn't see anything.

The problem with removing all of them and putting specific ones back is that this is a machine shop client, and one machine might print to a funky barcode printer, and the machine next to it might print to some printer on the other side of the building. So, there's no pattern. I just want to essentially "move" the printers to the new server, being as transparent as possible.

Pat Richard, MCSE(2) MCSA:Messaging, CNA(2)
 
Ah - seems it's a case issue. The script was seeing \\SERVER\blahblahblah, and I was looking for
\\server\blahblahblah

How can I ignore case?

Pat Richard, MCSE(2) MCSA:Messaging, CNA(2)
 
Have a look at the LCase (or UCase) function.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
But don't those just look for a lower case (or upper case) version? I'm looking for something that will find it regardless of case. I know I can IF that and check both ways, but I was hoping to do it a little cleaner than that.

Also, I noticed that the script looks for a printer by name and not share name. Is there a way to look for that? My concern is that people will have renamed them on their workstation. I know I can install by share name....

Thanks for the help!

Pat Richard, MCSE(2) MCSA:Messaging, CNA(2)
 
My suggestion was:
If LCase("\\Server\BlahBlah") = LCase("\\SERVER\blahblah") Then

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
I'll second that. Using the lcase with an if is the same as ignoring case because you are forcing both values to be lower case.

I hope you find this post helpful.

Regards,

Mark
 
im on that bandwagon, i tend to have rules like

using CInt or CDbl in if statements when comparing numerics
using CStr(LCase( when comparing strings etc etc (unless of course you want to detect the difference ;-))
 
Okay - those suggestions make sense, and some quick testing shows that something like this works:

Code:
For LOOP_COUNTER = 0 To WSHPrinters.Count - 1 Step 2
	' Wscript.Echo WSHPrinters.Item(LOOP_COUNTER +1)
	If LCase(WSHPrinters.Item(LOOP_COUNTER +1)) = LCase("\\old2003\Canon Bubble-Jet BJC-6000") Then
  	WSHNetwork.RemovePrinterConnection "\\old2003\Canon Bubble-Jet BJC-6000",True,True    
    WSHNetwork.AddWindowsPrinterConnection "\\new2003\Canon Bubble-Jet BJC-6000"  
	End If
Next

I'm still interested in detecting if a printer is the default. I know how to set a printer to BE the default - just not detect IF a printer is the default.

Thanks for all the help!

Pat Richard, MCSE(2) MCSA:Messaging, CNA(2)
 
there have been a number of posts by tsuji regarding setting/querying default printer,,using WMI i believe
 
Thanks mrmovie for referring to my past posts which are without pretention. I cannot myself find them more often than not. Here is a condensed version I can jot down if you don't mind illustrating the essential. Free to expand it. (No pretention as always.)
[tt]
set cprtr=getobject("winmgmts:").instancesof ("win32_printer")
for each oprtr in cprtr
if ((oprtr.attributes or &h04)=oprtr.attributes) then
wscript.echo "default printer" & vbcrlf & "name:" & vbtab & oprtr.name & vbcrlf & "portname:" & vbtab & oprtr.portname
else
wscript.echo "non-default printer" & vbcrlf & "name:" & vbtab & oprtr.name & vbcrlf & "portname:" & vbtab & oprtr.portname
end if
next
set cprtr=nothing
[/tt]
 
it might look like i am splitting hairs but i would suggest this

For LOOP_COUNTER = 0 To WSHPrinters.Count - 1 Step 2
' Wscript.Echo WSHPrinters.Item(LOOP_COUNTER +1)
If LCase(WSHPrinters.Item(LOOP_COUNTER +1)) = LCase("\\old2003\Canon Bubble-Jet BJC-6000") Then
WSHNetwork.RemovePrinterConnection WSHPrinters.Item(LOOP_COUNTER +1),True,True
WSHNetwork.AddWindowsPrinterConnection "\\new2003\Canon Bubble-Jet BJC-6000"
End If
Next

the difference is subtle but important i think.

if you are lucky that you have created the new print q names the same as the old ones (other than print servername) then your alogrithm will not need to worry about the actual printername, you will be able to do a string comparision finding the old print server name and do a Replace() with the new name then map to that...if you get my drift.

i got annoyed with WshNetwork and removing printer connections so i deleted printers by hacking the registry directly
 
tsuji, expanding on your code would only ruin the simplicity of it
 
Just a thought on this too, has the old server gone away? If so you can duplicate the share names and simply create a new host record in DNS with the old server name and point it to the new server IP. All existing mappings would then still work.

You may also want to look at the PrintMig 3.1 utility from Microsoft that is used for migrating printers from one server to another.


I hope you find this post helpful.

Regards,

Mark
 
MRMovie -

Thanks for the info. Actually, I would prefer to add & remove by share name, since that's a constant. Users could rename the printers on their workstation, but can't rename the share. Using the share ensures better results (IMHO). I do like how you changed the line that removes the printer. That makes total sense.

Pat Richard, MCSE(2) MCSA:Messaging, CNA(2)
 
no worries 58sniper,
interesting comment about users changing the printer names, just tried it myself on a mapped printer to a networked shared printer and i am unable to change the name, but there you go.
i went for the following printer functionality

+ ability to map printers based on AD group
+ ability to remove printers by supplying servername(i.e. removes all printers connected to a particular server)
+ ability to remove printers based on fq name
+ ability to swap printers based on old and new print server name

...no ever used all of them of course ;-), well perhaps once

good luck
 
Well, the client's environment is pretty messy. I'm just trying to streamline my part of the migration while their normal IT person is away.

But, since we do migrations and deployments every day, I'm just trying to cover my bases.

My other thought is about printers that have the same name, but different share names.

Pat Richard, MCSE(2) MCSA:Messaging, CNA(2)
 
You may want to look at this. It does a good job of parsing the names, installing the new connections, then deleting the old. There's a posting further down the page by "murbot" that is a little cleaner and handles going to 2 new servers from 1 old server.


I have a far more detailed version I've been working on, that should be complete by next week. I'll be glad to post a generic copy if you still need.

Cheers!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top