Solved

Script to delete local printer port

Posted on 2009-04-14
30
2,486 Views
Last Modified: 2012-05-06
I need a vb script to delete a local printer port.  Through Googling I've found lots of ways to delete TCP/IP Ports, but nothing about deleting a local port.
0
Comment
Question by:BalchBingham
  • 10
  • 10
  • 4
  • +2
30 Comments
 
LVL 23

Expert Comment

by:Danny Child
ID: 24143876
0
 
LVL 23

Expert Comment

by:Danny Child
ID: 24143880
apologies, looked again, that was IP.... sorry, pretty late round here.
0
 

Author Comment

by:BalchBingham
ID: 24143906
No problem, I already found almost that exact same script on TechNet.  Works great for IP, not so good with local...
Had high hopes for the code below, but it wasn't able to remove the port either.  Still searching....

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colInstalledPrinters = objWMIService.ExecQuery _
    ("Select * From Win32_Printer") 
For Each objPrinter in colInstalledPrinters
    If objPrinter.Portname = "Test" Then
        objPrinter.Delete_
    End If
Next
 
msgbox "Finished"

Open in new window

0
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
LVL 23

Expert Comment

by:Danny Child
ID: 24143940
too late for me now, will check again tomorrow... sorry!
0
 
LVL 23

Expert Comment

by:Danny Child
ID: 24143968
last call....:

http://support.microsoft.com/kb/321025

I know it's 2k, but most of that old good stuff still works....
0
 

Author Comment

by:BalchBingham
ID: 24152907
Can you give me an example?  Does PrinterAdmin  work with XP?
0
 

Author Comment

by:BalchBingham
ID: 24160329
I'm still looking for a solution that will allow me to script the deletion of a local printer port.  Any help?  It has to be scripted because it will be pushed out to 500+ computers.  Thanks in advance.
0
 
LVL 17

Expert Comment

by:houssam_ballout
ID: 24191737
0
 
LVL 14

Assisted Solution

by:yehudaha
yehudaha earned 150 total points
ID: 24192554
try this:
strComputer = "."
 
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
 
Set colInstalledPrinters = objWMIService.ExecQuery _
    ("Select * From Win32_Printer")
 
For Each objPrinter in colInstalledPrinters
    If Left(objPrinter.Portname, 3) = "LPT" Or Left(objPrinter.Portname, 3) = "USB" Then
        objPrinter.Delete_
    End If
Next

Open in new window

0
 
LVL 23

Expert Comment

by:gecko_au2003
ID: 24193366
Ensuring that on the objPrinter.Delete_ Line you have the underscore at the end ie

objPrinter.Delete_

as apposed to

objPrinter.Delete

Also change the DeviceID to your device id and it should work
 ("Select * from Win32_Printer where DeviceID = 'hp 2000c'")  
Replace hp 2000c as per above between the single quotes to what your device id is.
 
 

strComputer = "." 
Set objWMIService = GetObject("winmgmts:" _ 
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 
Set colInstalledPrinters = objWMIService.ExecQuery _ 
("Select * from Win32_Printer where DeviceID = 'hp 2000c'") 
For Each objPrinter in colInstalledPrinters 
objPrinter.Delete_ 
Next 

Open in new window

0
 

Author Comment

by:BalchBingham
ID: 24194874
These look like great ways to delete a printer, but I need to delete the actual port.  See the attached screen shot, I may not have been very clear in my purpose.
Port.JPG
0
 
LVL 23

Expert Comment

by:gecko_au2003
ID: 24195319


FYI you can remove an obsolete/invalid printer port by deleting the appropriate registry key under

HKLM\SYSTEM\ControlSet001\Control\Print\Monitors\Standard TCP/IP Port\Ports\
0
 

Author Comment

by:BalchBingham
ID: 24196135
Good to know, but this is a local port not an IP port.  Near that same key is a listing for:
HKLM\SYSTEM\ControlSet001\Control\Print\Monitors\Local Port, but it does not contain a listing of local ports.
0
 
LVL 23

Expert Comment

by:gecko_au2003
ID: 24196235
paq

http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_24135194.html
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
 
Set colInstalledPrinters =  objWMIService.ExecQuery _
    ("Select * from Win32_Printer Where Network = FALSE")
 
For Each objPrinter in colInstalledPrinters
    objPrinter.Delete_
Next

Open in new window

0
 

Author Comment

by:BalchBingham
ID: 24196366
The symantics in the code below aren't correct, but I hope it atleast shows what I'm trying to do.  Anyway you could correct the verbage for me?
Also, in the kb article you cited above, only one of the persons questions were answered.  

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
 
Set colInstalledPorts =  objWMIService.ExecQuery _
    ("Select * from Win32_LocalPrinterPort Where Name = 'Test'")
 
For Each objPort in colInstalledPorts 
    objPort.Delete
Next

Open in new window

0
 
LVL 14

Expert Comment

by:yehudaha
ID: 24197418
where did you find the lost Win32_LocalPrinterPort ?
0
 

Author Comment

by:BalchBingham
ID: 24200234
Ha, I just made it up!  (I said the syntax was wrong...)  Everything posted so far has had to do with IP Printers/Ports, and I was hoping it would help someone think of something I could use.  The WMI index is hard to search/use...  Even though it's a made up call, itt's the kind of script I need.
So is it possible to delete a local port?
0
 
LVL 23

Expert Comment

by:gecko_au2003
ID: 24202000
Not sure I will have a look later although whilst your waiting you may be able to solve the issue using

http://www.microsoft.com/downloads/details.aspx?FamilyID=2cc30a64-ea15-4661-8da4-55bbc145c30e&displaylang=en

wmi code creator ( Am pretty sure its that ) will test it later.

If you check the win32_printer class and the other classes that wmi uses for deleting a tcp ip printer port you may find a specific one to delete a local port.

Have been playing around with it a little bit at work but have been busy - I have this friday off so I can have a better attempt on friday as I wont be at work.

For now I hope this helps and appologise for taking a while
0
 
LVL 14

Expert Comment

by:yehudaha
ID: 24242605
i searched for a solution to your problem and nothing came out without deleting the printer itself.
if another expert can prove other wise i will be more than happy to hear it.
0
 
LVL 14

Expert Comment

by:yehudaha
ID: 24336658
hey

maybe i wrong but my last post is an answer to the question even if it's not a solution to complete the task asked for.

yehudaha

0
 
LVL 23

Expert Comment

by:gecko_au2003
ID: 24337004
I have no objections - although I am guessing it would be more of an API that you would require most likely to be able to just delete the port I am guessing as I had the same problem using WMI with regards to deleting the whole printer to delete the port or not at all so not sure if you would be interested in this
0
 

Author Comment

by:BalchBingham
ID: 24337415
I appreciate everyone's help and input.  I've done lots of testing here and it looks like leaving the port isn't going to cause any conflicts with the upgrade.  I'm not familar with API's, but if you have an example readily available I would be interested in seeing it.  Thanks again for the help, I really appreciate everyone's time!
0
 
LVL 23

Expert Comment

by:gecko_au2003
ID: 24337925
You can't use an API in scripting languages its more for an actual programming language ( I am not that great with programming ) but you may be able to include it in an ocx control or dll or something along those lines and possibly call it from vbscript ?
0
 
LVL 23

Assisted Solution

by:gecko_au2003
gecko_au2003 earned 350 total points
ID: 24338040
http://www.sxlist.com/techref/os/win/api/win32/func/src/f15_15.htm

and

http://winapi.freetechsecrets.com/win32/WIN32DeletePort.htm

You will have to use EnumPorts to enumerate all the valid ports and use DeletePort ( this will only allow you to use a programming language not a scripting language to use the above ) to enumerate and then delete a selected port so you may be able to use vb 6 or something else to create an OCX file which you could then call from vbscript to do what you wanted ?
0
 
LVL 23

Accepted Solution

by:
gecko_au2003 earned 350 total points
ID: 24338104
Looking around also reveals that before you delete it you will have to change the port so in essence

enumerate all valid ports

change the current port from local port that you want to delete to another port ( whether its local or not should not be an issue seeing as you just want it to select another port )

Then use Delete Port API to delete the port you want by selecting it and then if you want it to use another port you can use the change port again or you can select the correct port in the middle step

this website has a couple of examples that may lead you in the right direction

http://binaryworld.net/Main/CodeDetail.aspx?CodeId=3751
0
 

Author Closing Comment

by:BalchBingham
ID: 31570224
Thanks for everyones time and effort, I really appreciate.  
0
 
LVL 23

Expert Comment

by:gecko_au2003
ID: 24338323
no problem - just a shame there is not an ocx that I could find to do it - If I get enough time I will look into creating an ocx in vb 6 that will do the above If I can figure it out and If I can I will post back but might be a while.
0
 

Author Comment

by:BalchBingham
ID: 24339056
I do not have access to Visual Studio which is why I was trying to script it.  As I said earlier, leaving the port ended up not causing a conflict in our environment.  If you wanted to do it as a pet projection though that would be great and I'd be really interested in seeing it as I have no experience in making an ocx.
Thanks again,
Stephen
0
 
LVL 23

Expert Comment

by:gecko_au2003
ID: 24372045
I have not had time to look into the ocx file but have found this on the randy birch site for visual basic

http://vbnet.mvps.org/index.html?code/system/addports.htm

That should get you started with regards to using vb 6 or porting it into vb dot net ( vb dot net 2008 ) which you can get a free version for.

If I get time at some point I will look into doing an ocx or something along those lines as obviously need to research into it as I have not done that before
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

Suggested Solutions

This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
When I recently replaced my image transfer kit on my office HP color laserjet 5550dn printer, I had a slight problem.  The left bracket that holds the transfer kit got stuck in the upright locked position instead of being at a 45 degree angle facing…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

839 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