Script to delete local printer port

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.
BalchBinghamAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
gecko_au2003Connect With a Mentor Commented:
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
 
Danny ChildIT ManagerCommented:
0
 
Danny ChildIT ManagerCommented:
apologies, looked again, that was IP.... sorry, pretty late round here.
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
BalchBinghamAuthor Commented:
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
 
Danny ChildIT ManagerCommented:
too late for me now, will check again tomorrow... sorry!
0
 
Danny ChildIT ManagerCommented:
last call....:

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

I know it's 2k, but most of that old good stuff still works....
0
 
BalchBinghamAuthor Commented:
Can you give me an example?  Does PrinterAdmin  work with XP?
0
 
BalchBinghamAuthor Commented:
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
 
yehudahaConnect With a Mentor Commented:
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
 
gecko_au2003Commented:
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
 
BalchBinghamAuthor Commented:
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
 
gecko_au2003Commented:


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
 
BalchBinghamAuthor Commented:
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
 
gecko_au2003Commented:
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
 
BalchBinghamAuthor Commented:
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
 
yehudahaCommented:
where did you find the lost Win32_LocalPrinterPort ?
0
 
BalchBinghamAuthor Commented:
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
 
gecko_au2003Commented:
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
 
yehudahaCommented:
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
 
yehudahaCommented:
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
 
gecko_au2003Commented:
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
 
BalchBinghamAuthor Commented:
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
 
gecko_au2003Commented:
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
 
gecko_au2003Connect With a Mentor Commented:
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
 
BalchBinghamAuthor Commented:
Thanks for everyones time and effort, I really appreciate.  
0
 
gecko_au2003Commented:
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
 
BalchBinghamAuthor Commented:
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
 
gecko_au2003Commented:
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
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.