Solved

Script to delete local printer port

Posted on 2009-04-14
30
2,386 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:DanCh99
Comment Utility
0
 
LVL 23

Expert Comment

by:DanCh99
Comment Utility
apologies, looked again, that was IP.... sorry, pretty late round here.
0
 

Author Comment

by:BalchBingham
Comment Utility
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
 
LVL 23

Expert Comment

by:DanCh99
Comment Utility
too late for me now, will check again tomorrow... sorry!
0
 
LVL 23

Expert Comment

by:DanCh99
Comment Utility
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
Comment Utility
Can you give me an example?  Does PrinterAdmin  work with XP?
0
 

Author Comment

by:BalchBingham
Comment Utility
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
Comment Utility
0
 
LVL 14

Assisted Solution

by:yehudaha
yehudaha earned 150 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility


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
Comment Utility
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
Comment Utility
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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 

Author Comment

by:BalchBingham
Comment Utility
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
Comment Utility
where did you find the lost Win32_LocalPrinterPort ?
0
 

Author Comment

by:BalchBingham
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks for everyones time and effort, I really appreciate.  
0
 
LVL 23

Expert Comment

by:gecko_au2003
Comment Utility
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
Comment Utility
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
Comment Utility
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now