apeo Posted January 11, 2007 Report Posted January 11, 2007 \\server\printer are network printers and as geoff said will be removed with the standard script, so are they not removed? What is the whole script you are running? You might want to add in some error traps to see if there are any errors, or remove the 'On Error Resume Next'.
timbo343 Posted January 11, 2007 Report Posted January 11, 2007 This is what im running at the mo ' Script to add network printers to workstations and thin clients ' ' Author: Ric Charlton, 07/07/04 (amended 26/08/05) ' Declare variables and enumerate existing printer connections On Error Resume Next Set WshShell = WScript.CreateObject("WScript.Shell") Set WshNetwork = WScript.CreateObject("WScript.Network") Set oPrinters = WshNetwork.EnumPrinterConnections Dim computerName computerName = LCase(WshNetwork.ComputerName) WScript.Echo "computername variable: " & (Left(computerName, 4)) Select Case (Left(computerName, 4)) Case "pand" ' Delete Printer For i = 0 to oPrinters.Count - 1 step 2 On Error Resume Next wshNetwork.RemovePrinterConnection oPrinters.Item(i+1), true, true next WshNetwork.AddwindowsPrinterConnection "\\hades\R13-2015" WshNetwork.setDefaultPrinter "\\hades\R13-2015" End Select The only part that doesnt work is "wshNetwork.RemovePrinterConnection oPrinters.Item(i+1), true, true" that will not delete local printers
ChrisH Posted January 11, 2007 Report Posted January 11, 2007 Take out the On Error Resume Next and see if you get any error messages.
timbo343 Posted January 11, 2007 Report Posted January 11, 2007 i dont get any error messages, it deletes the \\hades\R13-2015 printer and puts it back but thats not the correct printer to delete. Ive got a \\hades\LY1-2200 local port printer and its not deleting it.
ChrisH Posted January 11, 2007 Report Posted January 11, 2007 This reminds me of a problem I had where if you had manually installed the printer (ie unscripted) the script would not delete the printer. Its something to do with the i+1 . I shall see if I can find my old post on another forum to see if it is relevant.
timbo343 Posted January 11, 2007 Report Posted January 11, 2007 ive got full permissions on this computer. Can i just ask, what does <> mean, if i put in If Left(oPrinters.Item(i), 3) <> "LY1" then wshNetwork.RemovePrinterConnection oPrinters.Item(i+1), true, true end if just want to know what symbol i would put in to replace <> that will equal LY1
timbo343 Posted January 11, 2007 Report Posted January 11, 2007 ahhh, thanks chris, yeah i had installed the printer manually, if you could have a look that would be great.. Thanks Tim
ChrisH Posted January 11, 2007 Report Posted January 11, 2007 What I was thinking off isnt going to help. Basically i= the IP Address and i +1= the share name. So how this works with local ports im not sure. Try echoing out the printer found within the loop with wscript.echo i & i+1 .
apeo Posted January 11, 2007 Report Posted January 11, 2007 In my printer script ive removed the 'step 2' out the for loop and i just use i instead of of i+1 i.e. FOR Counter = 0 to oPrinters.Count - 1 IF mid(oPrinters.Item(Counter), 1, 2) = "\\" THEN PrinterPath = oPrinters.Item(Counter) WshNetwork.RemovePrinterConnection PrinterPath, True, True End If Next cant remember why i did that though :oops:. With mine it looks for network printers so if i wanted to remove all printers then all i need to do is remove the if statement. Just to be certain, the printer you are trying to remove... is it Parallel or USB?
timbo343 Posted January 11, 2007 Report Posted January 11, 2007 im trying to get rid of a printer that is set up manually on a computer by local port with the port name of \\server\printer. The printer isnt physically connected to the machine, its attached to a print server on the network, hence the path and port name \\server\printer. Looks like ill have to go round all computers and delete the printer manually.
apeo Posted January 11, 2007 Report Posted January 11, 2007 Oh right gotcha... sorry i was being thick, you had said that before. ok try my script ie remove the step 2 and the i+1. if that doesnt work run these scripts to see what is outputed. 1: on error resume next Set WshNetwork = WScript.CreateObject("WScript.Network") dim Counter, PrinterPath dim strConnectString, strResult 'Remove All Network Printers. Set oPrinters = WshNetwork.EnumPrinterConnections For i = 0 to oPrinters.Count - 1 Step 2 wscript.echo oPrinters.Item(i+1) Next 2: on error resume next Set WshNetwork = WScript.CreateObject("WScript.Network") dim Counter, PrinterPath dim strConnectString, strResult 'Remove All Network Printers. Set oPrinters = WshNetwork.EnumPrinterConnections FOR Counter = 0 to oPrinters.Count - 1 WScript.Echo oPrinters.Item(Counter) Next
apeo Posted January 11, 2007 Report Posted January 11, 2007 :oops: seems that ive done multiple posts some how when i edited the post.. sorry. Can Edugeek admins pls delete the duplicates.
timbo343 Posted January 11, 2007 Report Posted January 11, 2007 Ive taken out the step 2 and the i+1 but nothing happened, then code 1. gives me outputs of what printers i have. With regards to the LY1 local port printeri have got an output of LY1-2200. then Code 2. give me outputs of the ports the printers are using as well as the printer names. With regards to the locally installed printer, the first message is \\hades\ly1-2200 and the second message is LY1-2200. Obviously the first one is the port name and the second is the name of the printer.
apeo Posted January 11, 2007 Report Posted January 11, 2007 Ok something odd is going on here as it doesnt look like anything is wrong with your script but you could try and remove just that printer by doing to following without the for loop: WshNetwork.RemovePrinterConnection "\\hades\ly1-2200", True, True that should just remove your printer, hope it works for ya.
timbo343 Posted January 11, 2007 Report Posted January 11, 2007 still dont work.. sorry.. just done a google search and found this article, but cant make heads or tails of the script (as im new to scripting) http://www.visualbasicscript.com/m_1208/tm.htm
timbo343 Posted January 11, 2007 Report Posted January 11, 2007 I think am going to delete the printer from the machines, as that looks like a regedit hack
apeo Posted January 11, 2007 Report Posted January 11, 2007 Ok i know why you cant delete the printer... its because when you try and delete \\hades\ly1-2200, it tries to delete a network printer so here is what i found: strComputer = "." Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set colInstalledPrinters = objWMIService.ExecQuery _ ("Select * from Win32_Printer where DeviceID = 'LY1-2200'") For Each objPrinter in colInstalledPrinters objPrinter.Delete_ Next I tested it and it works. Ive changed it to suit your situation.
apeo Posted January 11, 2007 Report Posted January 11, 2007 Just read what you found and it seems that what i put doesnt always work so they did indeed do a reghack in the end using a script but have you tried the initial suggestion? ie the code above.
timbo343 Posted January 20, 2007 Report Posted January 20, 2007 One last querry about this printer script im doing. We have lots of printers installed locally that were manually installed, so i was wondering if i have to type out trComputer = "." Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set colInstalledPrinters = objWMIService.ExecQuery _ ("Select * from Win32_Printer where DeviceID = 'LY1-2250'") For Each objPrinter in colInstalledPrinters objPrinter.Delete_ for each and every printer on our network, or if there is something that can be added into the scrpit so that it will delete multiple printers instead of having lots of the above command? Tim
ChrisH Posted January 20, 2007 Report Posted January 20, 2007 Just ommiting the where condition from the select statement should do it eg ("Select * from Win32_Printer) This will obviously find all printers though.
timbo343 Posted January 20, 2007 Report Posted January 20, 2007 ah ok, well i didnt make my self very clear, there is one local printer that needs to be kept which is called cutePDF, what i have got at the mo is ' Delete Local manually installed Printers 'Printer 1 strComputer = "." Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set colInstalledPrinters = objWMIService.ExecQuery _ ("Select * from Win32_Printer where DeviceID = 'SR-4650'") For Each objPrinter in colInstalledPrinters objPrinter.Delete_ next ' Printer 2 strComputer = "." Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set colInstalledPrinters = objWMIService.ExecQuery _ ("Select * from Win32_Printer where DeviceID = 'LY1-2250'") For Each objPrinter in colInstalledPrinters objPrinter.Delete_ next is there any way i can get both these printers to delete within one command instead of having two commands as shown, otherwise it will be a very long script and ive heard that, that way is a bad way of writing scripts Tim
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now