Welcome !
         

 Delete local printers using a script

Author Message
ZenKenobi

  • Total Posts : 10
  • Scores: 0
  • Reward points : 0
  • Joined: 8/3/2004
  • Location: USA
  • Status: offline
Delete local printers using a script Tuesday, August 03, 2004 11:05 AM (permalink)
0
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
 
#1
    mbouchard

    • Total Posts : 2164
    • Scores: 35
    • Reward points : 0
    • Joined: 5/15/2003
    • Location: USA
    • Status: offline
    Re: Delete local printers using a script Tuesday, August 03, 2004 12:07 PM (permalink)
    0
    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
     
    #2
      ZenKenobi

      • Total Posts : 10
      • Scores: 0
      • Reward points : 0
      • Joined: 8/3/2004
      • Location: USA
      • Status: offline
      Re: Delete local printers using a script Wednesday, August 04, 2004 3:11 AM (permalink)
      0
      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
       
      #3
        mbouchard

        • Total Posts : 2164
        • Scores: 35
        • Reward points : 0
        • Joined: 5/15/2003
        • Location: USA
        • Status: offline
        Re: Delete local printers using a script Wednesday, August 04, 2004 6:14 AM (permalink)
        0
        Did you change the device ID to equal your printer?
         
        #4
          ZenKenobi

          • Total Posts : 10
          • Scores: 0
          • Reward points : 0
          • Joined: 8/3/2004
          • Location: USA
          • Status: offline
          Re: Delete local printers using a script Wednesday, August 04, 2004 8:46 AM (permalink)
          0
          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.
           
          #5
            mbouchard

            • Total Posts : 2164
            • Scores: 35
            • Reward points : 0
            • Joined: 5/15/2003
            • Location: USA
            • Status: offline
            Re: Delete local printers using a script Thursday, August 05, 2004 1:35 AM (permalink)
            0
            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
             
            #6
              ZenKenobi

              • Total Posts : 10
              • Scores: 0
              • Reward points : 0
              • Joined: 8/3/2004
              • Location: USA
              • Status: offline
              Re: Delete local printers using a script Thursday, August 05, 2004 2:43 AM (permalink)
              0
              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.
               
              #7
                mbouchard

                • Total Posts : 2164
                • Scores: 35
                • Reward points : 0
                • Joined: 5/15/2003
                • Location: USA
                • Status: offline
                Re: Delete local printers using a script Thursday, August 05, 2004 6:25 AM (permalink)
                0
                Was the Provider error for the line that contained the delete?
                 
                #8
                  ZenKenobi

                  • Total Posts : 10
                  • Scores: 0
                  • Reward points : 0
                  • Joined: 8/3/2004
                  • Location: USA
                  • Status: offline
                  Re: Delete local printers using a script Thursday, August 05, 2004 10:57 AM (permalink)
                  0
                  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)
                   
                  #9
                    mbouchard

                    • Total Posts : 2164
                    • Scores: 35
                    • Reward points : 0
                    • Joined: 5/15/2003
                    • Location: USA
                    • Status: offline
                    Re: Delete local printers using a script Friday, August 06, 2004 1:10 AM (permalink)
                    0
                    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
                     
                    #10
                      ZenKenobi

                      • Total Posts : 10
                      • Scores: 0
                      • Reward points : 0
                      • Joined: 8/3/2004
                      • Location: USA
                      • Status: offline
                      Re: Delete local printers using a script Tuesday, August 10, 2004 5:06 AM (permalink)
                      0
                      It worked great. Many thanks for all your help!
                       
                      #11
                        Online Bookmarks Sharing: Share/Bookmark

                        Jump to:

                        Current active users

                        There are 0 members and 1 guests.

                        Icon Legend and Permission

                        • 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
                        • Read Message
                        • Post New Thread
                        • Reply to message
                        • Post New Poll
                        • Submit Vote
                        • Post reward post
                        • Delete my own posts
                        • Delete my own threads
                        • Rate post

                        2000-2014 ASPPlayground.NET Forum Version 3.9