Page 1 of 1 1
Topic Options
#143295 - 2005-07-09 01:10 AM Printer and Print Driver Removal Script
Kdyer Offline
KiX Supporter
*****

Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
Got the following Script to work, but needs to be run twice to remove the drivers:

Code:

cls
break on
dim $priReg,$i,$DriverKey,$Printers,$rc,$objServer,objService,$

$DriverKey='HKLM\SYSTEM\CurrentControlSet\Control\Print\Environments\Windows NT x86\Drivers\Version-3'
$Printers='HKCU\Software\Microsoft\Windows NT\CurrentVersion\Devices'

?'Before continuing, this process will remove all network printers and drivers'
?'Press CTRL+BREAK to stop now..'
?'Press any other key to continue'
?
get $

?'List Printers found..'
;Delete Printers
$i=0
DO
IF '\\'=LEFT($x,2) ; -- ONLY NETWORK PRINTERS
$priReg=$priReg+$x
ENDIF
$x=ENUMVALUE($Printers,$i)
IF '\\'=LEFT($x,2)
?$x
$rc=DELVALUE('HKCU\Software\Microsoft\Windows NT\CurrentVersion\Devices',$x)
$rc=DELVALUE('HKCU\Software\Microsoft\Windows NT\CurrentVersion\PrinterPorts',$x)
SLEEP '0.5'
ENDIF
$i=$i+1
UNTIL @error

??'Deleting Printers'
DelPrinterConnections

$objServer=GetObject('WinNT://.,computer')
$objService=$objServer.GetObject('Service','spooler')


??'Stopping the print spooler'
$objService.Stop
SLEEP 2

??'Listing Drivers'
;Delete drivers
$i=0
DO
$x=ENUMkey($DriverKey,$i)
IF 'HP'=LEFT($x,2) AND NOT INSTR($x,'Mobile')
?$x
SLEEP 1
ENDIF
$i=$i+1
UNTIL @error

??'Removing Drivers'
;Delete drivers
$i=0
DO
$x=ENUMkey($DriverKey,$i)
IF 'HP'=LEFT($x,2)
PDRIVERREMOVE(,$x)
SLEEP 1
ENDIF
$i=$i+1
UNTIL @error

SLEEP 2

??'Starting the print spooler'
$objService.Start

??'Process is complete, Press a key..'
$objServer=0
$objService=0
get $
;ActiveTouch
;CutePDF
;Image Writer
;Acrobat
;RightFAX
;Generator

; FUNCTION PDRIVERREMOVE()
;
; ACTION Remove Printer Drivers from a system
;
; AUTHOR Kent Dyer (leptonator@hotmail.com)
;
; CONTRIBUTORS
;
; VERSION 1.1
;
; DATE CREATED 29-June-2004
;
; DATE MODIFIED 24-May-2005
;
; KIXTART 4.02
;
; SYNTAX PdriverRemove()
;
; PARAMETERS
; wksta
; Workstation Name
; pdriver
; Print Driver Name
;
; DEPENDENCIES None
;
; RETURNS: Nothing
;
; REMARKS This is to remove print drivers by Microsoft Tech Article - 135406.
; http://support.microsoft.com/?id=135406
; This can be run locally using a login script, only written for 2k/XP/2k3
; If run using Login Script for example, you should remove the printers first
; Since this goes into HKLM for the Registry, and %WINDIR%\SYSTEM32, you will need
; appropriate priveleges. However, this can also be run as a remote Admin Script.
;
; EXAMPLEs
; PdriverRemove(,"HP LASERJET 4 PLUS") ; Remove HP LASERJET 4 PLUS from local Workstation
; PdriverRemove() ; Remove all Print Drivers from the system
;
; REMOVE PRINT DRIVERS FROM COMPUTERS ON THE DOMAIN
; DIM $container,$computer,$
; ?'Checking Domain computers now...'
; $container=GetObject('WinNT://'+@ldomain)
; $container.filter='Computer', ''
; FOR EACH $computer IN $container
; $computern=$computer.name
; ?$computern
; SHELL '%COMSPEC% /C PING -n 1 '+$computern
; IF @error=0
; PDRIVERREMOVE($srv)
; ENDIF
; NEXT
; ?'PROCESS IS COMPLETE.. PRESS A KEY'
; GET $
; KIXTART BBS http://www.kixtart.org/ubbthreads/showflat.php?Cat=&Number=139441

FUNCTION PDRIVERREMOVE(OPTIONAL $wksta, OPTIONAL $pdriver)
DIM $key,$keyname,$index,$fl,$rc,$drv
IF NOT $wksta
$wksta='\\'+@wksta+'\'
ELSE
$wksta='\\'+$wksta+'\'
ENDIF
$key=$wksta+'HKLM\SYSTEM\CurrentControlSet\Control\Print\Environments\Windows NT x86\Drivers\Version-3\'
IF $pdriver<>''
$index=0
DO
$keyname=ENUMKEY($key,$index)
IF INSTR($keyname,$pdriver)
$drv=READVALUE($key+$keyname,'Data File')
FOR EACH $fl IN SPLIT(READVALUE($key+$keyname,'Dependent Files'),'|')
IF EXIST($fl)
DEL $wksta+'admin$$\system32\spool\drivers\w32x86\3\'+$fl
ENDIF
NEXT
$rc=DELTREE($key+$keyname)
; -- AT THIS POINT, you should probably either restart the client or the PRINT SPOOLER
ENDIF
$index=$index+1
UNTIL LEN($keyname)=0
ELSE
; -- DELETE ALL PRINT DRIVERS
$index=0
DO
$keyname=ENUMKEY($key,$index)
$drv=READVALUE($key+$keyname,'Data File')
FOR EACH $fl IN SPLIT(READVALUE($key+$keyname,'Dependent Files'),'|')
IF EXIST($fl)
DEL $wksta+'admin$$\system32\spool\drivers\w32x86\3\'+$fl
ENDIF
NEXT
$rc=DELTREE($key+$keyname)
; -- AT THIS POINT, you should probably either restart the client or the PRINT SPOOLER
$index=$index+1
UNTIL LEN($keyname)=0
ENDIF
ENDFUNCTION

; -- Thanks Lonkero!
function DelPrinterConnections()
dim $c,$bk,$conn
$c=0
$bk='HKCU\Printers\Connections'
$conn=enumkey($bk,$counter)
while @error=0
$c=$c+1
$conn=delkey($bk+'\'+$conn)
$conn=enumkey($bk,$counter)
loop
endfunction



Thanks for any insight.

Kent
_________________________
Utilize these resources:
UDFs (Full List)
KiXtart FAQ & How to's

Top
#143296 - 2005-07-09 01:31 AM Re: Printer and Print Driver Removal Script
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
eh...
run twice?
you also got driver remove udf and the script before it does something as well...

as far as I remember, removing printer driver is matter of some 2-3 registry deletions and that's it.
_________________________
!

download KiXnet

Top
#143297 - 2005-07-09 10:06 PM Re: Printer and Print Driver Removal Script
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
I think it has to do with your use of ENUMVALUE/ENUNKEY while you're actively deleting values/keys. If you do an ENUMVALUE and you delete the second value, then automatically the third entry is now the second. So, if you then increase the eNUMVALUE by oen you will then get the fourth value. That's why I'd ratehr use ArrayEnumValue() and ArrayEnumKey() to get the keys/values first, then delete them. Otherwise, do the ENUMVALUE and if you're deleting that value, do not increase the index by one but reissue the ENUMVALUE with the same index.
_________________________
There are two types of vessels, submarines and targets.

Top
#143298 - 2005-07-23 04:55 AM Re: Printer and Print Driver Removal Script
Kdyer Offline
KiX Supporter
*****

Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
Quote:

eh...
run twice?
you also got driver remove udf and the script before it does something as well...

as far as I remember, removing printer driver is matter of some 2-3 registry deletions and that's it.




So.. Lonk - Do you know what keys are required for the drivers? I don't want to nuke Acrobat, RightFAX, drivers etc.

Kent
_________________________
Utilize these resources:
UDFs (Full List)
KiXtart FAQ & How to's

Top
#143299 - 2005-07-23 05:27 AM Re: Printer and Print Driver Removal Script
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
well, for w2k and xp drivers, isn't it enough to remove the driver key and according files descriped in:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print\Environments\Windows NT x86\Drivers\Version-3\driverName

now, what comes to nuking local stuff, remove all that remove code you wrote above.
everything before "delPrinterConnections" should be gone.
as once you issue that, you will not have network printers connected on your system.

then, read from all of the printers that still are in the printers key, the "printer driver" value:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Print\Printers

against these you can compare the data in the first key (the driver description key) and delete all drivers that are not used anymore.
that simple.
_________________________
!

download KiXnet

Top
Page 1 of 1 1


Moderator:  Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart 
Hop to:
Shout Box

Who's Online
1 registered (Allen) and 1607 anonymous users online.
Newest Members
Viginette, ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder
17888 Registered Users

Generated in 0.053 seconds in which 0.024 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org