Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


Delete local printers using a script

 
Logged in as: Guest
arrSession:exec spGetSession 2,2,1208
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Delete local printers using a script
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1]
Login
Message << Older Topic   Newer Topic >>
 Delete local printers using a script - 8/3/2004 10:05:52 AM   
  ZenKenobi

 

Posts: 10
Score: 0
Joined: 8/3/2004
From: USA
Status: offline
Does anyone know how to delete a local printer using VB script or Jscript on a Windows 2000 box? I tried using WSHNetwork.RemovePrinterConnection method described on the TechNet website, but it only seems to work for network printer connections. I even tried using the UNC name for machine and printer name (\\Machine\Printer) and the RemovePrinterConnectionMethod, but i still get a run-time error. Is it even possible to delete a local printer using VB script?

Someone please enlighten me,

_____________________________

Erik Gardner
 
 
Post #: 1
 
 Re: Delete local printers using a script - 8/3/2004 11:07:13 AM   
  mbouchard


Posts: 1916
Score: 16
Joined: 5/15/2003
From: USA
Status: offline
Try WMI. I used tested this script and deleted a local printer

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

(in reply to ZenKenobi)
 
 
Post #: 2
 
 Re: Delete local printers using a script - 8/4/2004 2:11:51 AM   
  ZenKenobi

 

Posts: 10
Score: 0
Joined: 8/3/2004
From: USA
Status: offline
I tried the script and it I get a runtime error when I get to objPrinter.Delete (Object doesn't support this property or method). Do you happen to know another method used with this object that might work? Thanks very much for all your help

(in reply to ZenKenobi)
 
 
Post #: 3
 
 Re: Delete local printers using a script - 8/4/2004 5:14:17 AM   
  mbouchard


Posts: 1916
Score: 16
Joined: 5/15/2003
From: USA
Status: offline
Did you change the device ID to equal your printer?

(in reply to ZenKenobi)
 
 
Post #: 4
 
 Re: Delete local printers using a script - 8/4/2004 7:46:56 AM   
  ZenKenobi

 

Posts: 10
Score: 0
Joined: 8/3/2004
From: USA
Status: offline
Yes. Here are a few of the various scripts I have tried.

strComputer = "."
set objNetwork = CreateObject("Wscript.Network")
objNetwork.RemovePrinterConnection "\\" & strComputer & "\Symantec Fax Starter Edition", true, true

This script returns the error "parameter incorrect"

strComputer = "."
set objNetwork = CreateObject("Wscript.Network")
objNetwork.RemovePrinterConnection "OLFModem"

(OLFModem is the port the printer is attached to)This one gives me the error "This network connection does not exist"

Set objWMIService = Getobject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colInstalledPrinters = objWMIService.ExecQuery _
("SELECT * FROM win32_Printer WHERE DeviceID = 'Symantec Fax Starter Edition'")
For Each objPrinter in colInstalledPrinters
objPrinter.Delete
Next

This one returns an error "Object does not support property or method", Which it should because the Win32_Printer object doesnt support the Delete method (even though it really should). I know there has to be some kind of workaround, but I'm not the most proficient VBScript writer. Thanks for the help.

(in reply to ZenKenobi)
 
 
Post #: 5
 
 Re: Delete local printers using a script - 8/5/2004 12:35:41 AM   
  mbouchard


Posts: 1916
Score: 16
Joined: 5/15/2003
From: USA
Status: offline
Not really sure why this is but, I tested your script at work and it failed for me (Worked at home) then I got to looking in script center.
After running some tests I copied the script from Script Center and put in my test local printer (LPT1) and it worked. I then began comparing the script you posted and the one from SC and found 1 difference.
Yours was missing the _ at the end of the delete line. Try changing objPrinter.Delete to objPrinter.Delete_ and it should work. I tested it and it worked for me.

Mike

(in reply to ZenKenobi)
 
 
Post #: 6
 
 Re: Delete local printers using a script - 8/5/2004 1:43:22 AM   
  ZenKenobi

 

Posts: 10
Score: 0
Joined: 8/3/2004
From: USA
Status: offline
I originally used the underscore after delete, but I always get the error message "Provider is not capable of the attempted operation". I'm going to try the script on a different local printer and see if it is perhaps a problem with the one that I have been trying to delete. As always, I really appreciate your help.

(in reply to ZenKenobi)
 
 
Post #: 7
 
 Re: Delete local printers using a script - 8/5/2004 5:25:56 AM   
  mbouchard


Posts: 1916
Score: 16
Joined: 5/15/2003
From: USA
Status: offline
Was the Provider error for the line that contained the delete?

(in reply to ZenKenobi)
 
 
Post #: 8
 
 Re: Delete local printers using a script - 8/5/2004 9:57:02 AM   
  ZenKenobi

 

Posts: 10
Score: 0
Joined: 8/3/2004
From: USA
Status: offline
Yes it was on that line. I've thought of a different way to remove the printer, although I'm having a bit of trouble with it as well. If I go and delete the registry key located at HKLM\SYSTEM\CurrentControlSet\Control\Print\Printers\Symantec Fax Starter Edition, it removes the printer from my printers folder (which is the desired result for my end users). The only problem with this is, the key wont delete. Does the DeleteKey or RegDelete require that subkeys or entries and values are deleted first? The registry key I am trying to delete has 3 subkeys, all having Entries with values. Here is the code I have tried.

Set WSHShell=Wscript.CreateObject("WScript.Shell")
WSHShell.RegDelete "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print\Printers\Symantec Fax Starter Edition"

'(When I use the above script I get the error "Unable to delete registry key")

Const HKEY_LOCAL_MACHINE = &H80000002

strComputer = "."

Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
strComputer & "\root\default:StdRegProv")
strKeyPath = "\SYSTEM\CurrentControlSet\Control\Print\Printers\Symantec Fax Starter Edition"
objReg.DeleteKey HKEY_LOCAL_MACHINE, strKeyPath

'(When I run the above script, I dont get any errors, but I dont lose the registry key either)

(in reply to ZenKenobi)
 
 
Post #: 9
 
 Re: Delete local printers using a script - 8/6/2004 12:10:19 AM   
  mbouchard


Posts: 1916
Score: 16
Joined: 5/15/2003
From: USA
Status: offline
Zen,

Yes, you would need to delete all the subkeys to delete a regkey.

Here is a script that we use at work to delete regkeys and their subkeys.

Const HKEY_CURRENT_USER = &H80000001
Dim oReg
Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")

Call DelRegKey(oReg, HKEY_CURRENT_USER, "Software\Microsoft\OfficeCustomizeWizard")

Msgbox "Completed"

Sub DelRegKey(oReg, Key, sSubkey)
' Deletes registry key and subkeys (permissions to delete keys must exist)
' Key: Tree that contains the sSubKey path
' (HKEY_CLASSES_ROOT, HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE, HKEY_USERS or HKEY_CURRENT_CONFIG)
' sSubkey: Subkey to be deleted (Case-sensitive string)
' (Example: "SOFTWARE\System Admin Scripting Guide")
Dim aSubkeys, subkey
oReg.EnumKey Key, sSubkey, aSubkeys
If Not IsNull(aSubKeys) Then
For Each subkey In aSubkeys
Call DelRegKey(oReg, Key, sSubKey & "\" & subkey)
Next
End If
' Error trapping in case we can't delete (no permissions)
On Error Resume Next
oReg.DeleteKey Key, sSubKey
On Error GoTo 0
End Sub

(in reply to ZenKenobi)
 
 
Post #: 10
 
 Re: Delete local printers using a script - 8/10/2004 4:06:57 AM   
  ZenKenobi

 

Posts: 10
Score: 0
Joined: 8/3/2004
From: USA
Status: offline
It worked great. Many thanks for all your help!

(in reply to ZenKenobi)
 
 
Post #: 11
 
 
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Delete local printers using a script Page: [1]
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts