Jump to content

issues with windows 2000pro printer script


Recommended Posts

Below is the code i have to delete all printers from windows xp/2000 machines. The code for xp works perfectly, I have the wmi code for the 2000 boxes because the attribute (-x) does not exist on them. When i run the code on a windows 2k macine i get an error

"AutoIt Error"

Line -1:

Error: The requested action with this object has failed.

I copied the code from someones blog, and am not familiar enough with wmi to figure out whats wrong. Any insight would be much appreciated.

Func DELPR()
 MsgBox(0,"Removing all Printers","Please wait - Uninstalling local printers",1)
If @OSVersion="WIN_XP" Then
If $SystemRoot = "C:\WINNT" Then
    RunWait(@COMSPEC & " /c c:\winnt\System32\cscript.exe Z:\printer\prnmngr.vbs -x")
Else
    RunWait(@COMSPEC & " /c c:\windows\System32\cscript.exe Z:\printer\prnmngr.vbs -x")
EndIf
EndIf

;prnmngr.vbs doesn't play well with windows 2000 for printer removal
;the wmi service doesnt support the remove_ function, so this is the script needed to do it.
If @OSVersion="WIN_2000" Then
$objNetwork = ObjCreate("WScript.Network")
$strComputer = "."
$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\cimv2")
$colPrinters = $objWMIService.ExecQuery("Select * From Win32_Printer")

For $objPrinter in $colPrinters
    If $objPrinter.Attributes Then
        $strPrinter = $objPrinter.Name
        $objNetwork.RemovePrinterConnection ($strPrinter)
    EndIf
Next
EndIf
Link to comment
Share on other sites

I added the com error function that i found in another post. The error message that i get now is.

We intercepted a COM Error !

Number is:80020009

Windescription is: This network connection does not exist.

Source is: WSHNetwork.RemoveNetworkDrive

Script line is: -1

I do have a line at the beginning of the code that maps a drive, and a line at the end that removes the mapping. but there is no way that those are being called as the user must click a button before they can run.

Link to comment
Share on other sites

I have decided to go about this in a different way. I am now trying to delete the printer and printer port from the registry.

If @OSVersion="WIN_2000" Then

; this sub deletes the printer entries for all printers on the local computer its run against.
;@Error Resume Next
Const $HKEY_LOCAL_MACHINE = "H80000002"

$strComputer = "."
$strKeyPath = "SYSTEM\SYSTEM\CurrentControlSet\Control\Print\Printers\"

 $objRegistry = ObjGet("winmgmts:\\" & $strComputer & "\root\default:StdRegProv")

DeleteSubkeys ()

;this sub deletes all of the TCP/IP ports on the local printer its run against.
;@Error Resume Next
$strComputer = "."
$strKeyPath = "SYSTEM\CurrentControlSet\Control\Print\Monitors\Standard TCP/IP Port\Ports\"

 $objRegistry = ObjGet("winmgmts:\\" & $strComputer & "\root\default:StdRegProv")

DeleteSubkeys ()

Func DeleteSubkeys($HKEY_LOCAL_MACHINE, $strKeyPath)
    $objRegistry.EnumKey ($HKEY_LOCAL_MACHINE, $strKeyPath,) <$arrSubkeys>

    If IsArray($arrSubkeys) Then
        For $strSubkey In $arrSubkeys
            DeleteSubkeys ($HKEY_LOCAL_MACHINE, $strKeyPath & "\" & $strSubkey )
        Next
    EndIf

    $objRegistry.DeleteKey ($HKEY_LOCAL_MACHINE, $strKeyPath )
EndFunc
EndIf

It shows the printers that its deleting, but doesnt delete them, I'm betting due to keys (values?) residing in them (sub-keys)?

How do i make this work?

Edited by dot45
Link to comment
Share on other sites

I think i have it narrowed down to one line of code:

$objRegistry.EnumKey ($HKEY_LOCAL_MACHINE, $strKeyPath,) <$arrSubkeys>

My syntax is not correct. The enumerated key needs to be put into $arrSubkeys, how should it be coded?

Link to comment
Share on other sites

  • Moderators

Quit bumping your post. If you do it again within 24 hours of your last post, you'll be taking a forum posting break for sometime, as well as your thread being locked and not being able to be replied to.

If you need to add something, use the edit feature!

Edit:

My main concern here was that there were 4 posts to this thread, and all for posters were you.

Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...