#76356 - 2003-08-13 06:08 PM
Capture, Delete, and re-add network printers
|
Kdyer
KiX Supporter
   
Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
|
Trying to create a script to capture all network printers to a file, delete them, and re-add..
However, it does not delete the drivers listed at -
HKLM\SYSTEM\CurrentControlSet\Control\Print\Environments\Windows NT x86\Drivers\Version-3
I think this needs some clean up as I am a bit stuck right now.
Here is the code -
code:
CLS BREAK ON
$ps=@scriptdir+'\Printsetup.ini' ?'Backing up all printers to a file' $j=0 DO ENUMVALUE('HKCU\Software\Microsoft\Windows NT\CurrentVersion\Devices',$j) IF primapstate($key)=2 $rc=WRITEPROFILESTRING($ps,'Printers','Default',$key) ELSE $i=0 DO IF '\\'=LEFT($x,2) $prireg=$prireg+$x ENDIF $x=ENUMVALUE($dev,$i) $i=$i+1 UNTIL @error IF LEN($prireg) $rc=WRITEPROFILESTRING($ps,'Printers','','') $prireg=split($prireg,'\\') FOR $i=1 TO ubound($prireg) $rc=WRITEPROFILESTRING($ps,'Printers',$i,'\\'+$prireg[$i]) NEXT ENDIF ENDIF $y=ENUMVALUE('HKCU\Software\Microsoft\Windows NT\CurrentVersion\Devices',$j) $j=$j+1 UNTIL @error
?'undoing lpt1' USE LPT1 /delete /persistent
?'Deleting all network printers' Delprinterconnections
?'Re-Mapping all network printers' REMAP($nprt,$ps)
?'Deleting work file' DEL $ps
?'All done - closing in two seconds' SLEEP 2
FUNCTION REMAP($nprt,$ps) DIM $rc, $nprt, $ps SELECT CASE INSTR($nprt,'\\') ;WRITE THE DEFAULT PRINTER TO THE CONFIG $rc=WRITEPROFILESTRING($ps,'Printers','Default',$nprt) USE LPT1: /delete /persistent USE LPT1: $nprt /persistent CASE $nprt='' AND EXIST($ps) ;NO DEFAULT PRINTER, LOAD PRINTERS FROM SAVED INFO FOR EACH $key IN split(readprofilestring($ps,'Printers',''),chr(10)) $nul=addprinterconnection(readprofilestring($ps,'Printers',$key)) NEXT $nprt=READPROFILESTRING($ps,'Printers','Default') $rc=SETDEFAULTPRINTER($nprt) USE LPT1: $nprt /persistent CASE NOT INSTR($nprt,'\\') ;LOCAL PRINTER DEFAULT, SO WE WANT TO REMOVE NETWORK MAPPING USE LPT1: /delete /persistent ENDSELECT ENDFUNCTION
FUNCTION DelPrinterConnections() DIM $c,$bk,$conn $c=0 $bk="HKCU\Printers\Connections" $conn=enumkey($bk,$counter) WHILE @error=0 $c=$c+1 $nconn='\\'+SPLIT($conn,',')[2]+'\'+SPLIT($conn,',')[3] $rc=DELPRINTERCONNECTION($nconn) $conn=delkey($bk+"\"+$conn) $conn=enumkey($bk,$counter) LOOP ENDFUNCTION
FUNCTION PriMapState($_pri) IF len(readvalue("HKCU\Software\Microsoft\Windows NT\CurrentVersion\Devices",$_pri)) IF instr(readvalue("HKCU\Software\Microsoft\Windows NT\CurrentVersion\Windows","Device"),$_pri) $primapstate=2 ELSE $primapstate=1 ENDIF ENDIF ENDFUNCTION
Thanks,
Kent
|
|
Top
|
|
|
|
#76357 - 2003-08-13 06:14 PM
Re: Capture, Delete, and re-add network printers
|
Sealeopard
KiX Master
   
Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
|
I don't think that DelPrinterConnections() will work correctly.
First of all, you're using both $c and $counter as indexing variable. Secondly, if you delete a registry key, then the next key moves up in the index. Thus deleting the key and increasing the index will only delete every other key. Jooels UDF seems to be wrong, too, in that regard.
_________________________
There are two types of vessels, submarines and targets.
|
|
Top
|
|
|
|
#76358 - 2003-08-13 06:21 PM
Re: Capture, Delete, and re-add network printers
|
Kdyer
KiX Supporter
   
Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
|
So... Then DELPRINTERCONNECTION would suffice..
I did notice, with Jooel's code "DELPRINTERCONNECTIONS" still retains the printers at -
HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Devices
Even though at HKCU\Printers\Connections they are removed.. This is why I added in the piece to capture the server\printer to finish it off.
Kent
|
|
Top
|
|
|
|
Moderator: Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart
|
0 registered
and 640 anonymous users online.
|
|
|