Jump to content

Printer Cleanup


Go to solution Solved by codeseeker,

Recommended Posts

I have been tasked with cleaning up tons of network printers off of a print server that will soon be repurposed. Unfortunately I can't wipe them all at once. My thoughts are to list all printers using something like this ( pulled this from another gentleman that posted how to list printers)

$wbemFlagReturnImmediately = "&h10"
$wbemFlagForwardOnly = "&h20"

$WMI = ObjGet("winmgmts:" & @ComputerName & "rootCIMV2")
$aItems = $WMI.ExecQuery("SELECT * FROM Win32_Printer", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)

For $printer in $aItems
 MsgBox(0, "Printers", $printer.Name)
Next

 

But instead of a message box I would like to save every 100 printers to a text file and have an appending name for every 100 in a list, for example printlist1.txt, printlist2.txt and so on...

Once I have created my printer list I then had an  idea to read the list into a variable and then delete each printer in the list similar to what I have in the code I created below that I scrape together off of examples and tutorials, except I don't know how to delete it from the list. In this example it just shows me the array.

#include <guiconstants.au3>
#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Outfile=SystemtoolsAutoitv3scriptsaccessrenamev1.exe
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <Array.au3>
#include <File.au3>

Global $strComputer = "imperial-leader"
Global $objWMIService = ObjGet("winmgmts:" & $strComputer & "rootCIMV2")
Global $collPrinters = $objWMIService.ExecQuery("Select * FROM Win32_Printer")
Global $aText ;create your array variable
_FileReadToArray(@DesktopDir & "Test.txt", $aText) ;read your text file into the array
_ArrayDisplay($aText) ;to verify your data is in the array
#For $i = 1 To $aText[0] ;loop through all of the elements in your array to perform whatever action you would like.
 #$printer = $aText[$i]
 #Next
For $i = 1 To $aText[0]
 $printerlist = $aText[$i]
Next
For $i = 0 to 4 - 1 ; We have an array with four elements but the last index is three.
 Next
 ConsoleWrite($aText[$i] & @LF)

If someone can assist me with what I'm trying to achieve either with better logic or adding to what I have, Please help! Thanks in advance

Link to comment
Share on other sites

This script will list every printer that a computer has.

$objNet = ObjCreate("Wscript.Network")
$WSHShell = ObjCreate("WScript.Shell")
$WSHPrinters = $objNet.EnumPrinterConnections;
For $LOOP_COUNTER = 0 To $WSHPrinters.Count - 1 Step 2
    ConsoleWrite($WSHPrinters.Item($LOOP_COUNTER + 1) & @CRLF)
Next

This script will delete all printers that a computer has.

$objNet = ObjCreate("Wscript.Network")
$WSHShell = ObjCreate("WScript.Shell")
$WSHPrinters = $objNet.EnumPrinterConnections;
For $LOOP_COUNTER = 0 To $WSHPrinters.Count - 1 Step 2
    ; To remove only networked printers use this If Statement
    If StringLeft($WSHPrinters.Item($LOOP_COUNTER + 1), 2) = "\\" Then
        $objNet.RemovePrinterConnection($WSHPrinters.Item($LOOP_COUNTER + 1), True, True)
    EndIf
    ; To remove all printers incuding LOCAL printers use this statement and comment
    ; out the If Statement above
    ;   $objNet.RemovePrinterConnection($WSHPrinters.Item($LOOP_COUNTER +1),True,True)
Next

There's a line that's commented out that can be used to delete local printers as well as networked printers, see the comments in the code.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

This script will list every printer that a computer has.

$objNet = ObjCreate("Wscript.Network")
$WSHShell = ObjCreate("WScript.Shell")
$WSHPrinters = $objNet.EnumPrinterConnections;
For $LOOP_COUNTER = 0 To $WSHPrinters.Count - 1 Step 2
    ConsoleWrite($WSHPrinters.Item($LOOP_COUNTER + 1) & @CRLF)
Next

This script will delete all printers that a computer has.

$objNet = ObjCreate("Wscript.Network")
$WSHShell = ObjCreate("WScript.Shell")
$WSHPrinters = $objNet.EnumPrinterConnections;
For $LOOP_COUNTER = 0 To $WSHPrinters.Count - 1 Step 2
    ; To remove only networked printers use this If Statement
    If StringLeft($WSHPrinters.Item($LOOP_COUNTER + 1), 2) = "\\" Then
        $objNet.RemovePrinterConnection($WSHPrinters.Item($LOOP_COUNTER + 1), True, True)
    EndIf
    ; To remove all printers incuding LOCAL printers use this statement and comment
    ; out the If Statement above
    ;   $objNet.RemovePrinterConnection($WSHPrinters.Item($LOOP_COUNTER +1),True,True)
Next

There's a line that's commented out that can be used to delete local printers as well as networked printers, see the comments in the code.

Thanks, but my issue is I cant delete all at once, I need to break the printers up into groups of 100. I was looking for a way to save all the printers listed into a file, but after 100 entries create a new file, and then delete printers listed in each file.

Link to comment
Share on other sites

What I posted was a way for you to see how it's done, and then to modify it for your needs. If you want someone to write it for you, then you should have asked that so I wouldn't have wasted my time showing you how YOU can write it.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

As you can see in the first example, the way to list the printers is by using $WSHPrinters.Item($LOOP_COUNTER + 1), so instead of using ConsoleWrite, you could use FileWriteLine to write the names to a text file one printer name at a time, and create a new text file every 100th printer name.

Then to delete them, you could parse the file(s) one line at a time using FileReadToArray, and loop through the lines to delete them one at a time.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

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...