I am working on the following script...----- start script
;
; This script is called from the logon script. It will remove all the network
; printers that have been added, then it will add the printers needed for the
; workstation that the user is logging into.
;
; Pat Hennessy - 2/15/2001
;
;
; Delete all network printers.
;
$index_one = 0
:del_printer_loop
$pinfo = enumkey("HKEY_CURRENT_USER\Printers\Connections\", $index_one)
? "$index_one"
if (@error = 0)
$pconnection = ""
$index_two = 1
do
$letter = substr($pinfo, $index_two, 1)
if ($letter = ",")
$pconnection = $pconnection + "\"
else
$pconnection = $pconnection + $letter
endif
$index_two = $index_two + 1
until ($index_two = len($pinfo) + 1)
? "$pconnection"
if delprinterconnection($pconnection)
$return = messagebox("An error occurred while removing $pconnection.", "Error removing printer (@error)", 4096 + 16)
endif
; $index_one = $index_one + 1
goto del_printer_loop
endif
;
; Delete old HKCU registry stuff
;
if (existkey("HKEY_CURRENT_USER\Software\DTCC") = 0)
$return = deltree("HKEY_CURRENT_USER\Software\DTCC")
endif
;
; Next we see if this workstation uses a windows print server.
;
if (existkey("HKEY_LOCAL_MACHINE\SOFTWARE\DTCC\Printing") = 0)
gosub "ntprint"
exit 0
else
exit 1
endif
;
; Thats all folks (except for the subroutine below)
;
exit 0
;
; Start of ntprint subroutine.
;
:ntprint
;
; Now we need to find and add the local printers.
;
$index = 0
:add_printer_loop
$val_name = enumvalue("HKEY_LOCAL_MACHINE\SOFTWARE\DTCC\Printing\Local Printers", $index)
? "$index"
if (@error = 0)
$new_printer = readvalue("HKEY_LOCAL_MACHINE\SOFTWARE\DTCC\Printing\Local Printers", $val_name)
? "$new_printer"
if (addprinterconnection($new_printer))
$return = messagebox("An error occurred while adding $new_printer.", "Error adding printer (@error)", 4096 + 16)
else
if (substr($new_printer, 13, 2) = "pl")
use lpt3: /del
use lpt3: "$new_printer"
if (@error > 0)
$return = messagebox("An error occurred while setting lpt3: to $new_printer.", "Error adding printer (@error)", 4096 + 16)
endif
endif
endif
$index = $index + 1
goto add_printer_loop
endif
;
; Next we need to determine the default printer.
;
$default_printer = readvalue("HKEY_LOCAL_MACHINE\SOFTWARE\DTCC\Printing", "Default Printer")
if (@error = 0)
if (setdefaultprinter($default_printer))
$return = messagebox("An error occurred while setting the default printer to $default_printer.", "Error setting default printer (@error)", 4096 + 16)
endif
if (substr($default_printer, 1, 2) = "\\")
use lpt2: /del
use lpt2: "$default_printer"
if (@error > 0)
$return = messagebox("An error occurred while setting lpt2: to $default_printer.", "Error setting default printer (@error)", 4096 + 16)
endif
endif
endif
; End of ntprint subroutine.
return
----- end script
The problem is using the enumkey() function in the del_printer_loop. I had to comment out the line to increment the $index_one variable because if I did increment $index_one it would only get the name of every other key. If $index_one stays at 0 the whole time, then it will read all of the keys. I've once played with perl modules that enumerate registry keys and I think they work the same way. Is this the expected behavior? The manual says that you need to use an index. I'm afraid to start using this on the domain if the script may hang on some machines.
Here is the output from running the script with the line incrementing the index commented out...
----- start
D:\temp>kix32 newprint
0
\\metroform\sp_a227
0
\\metroform\sp_a228
0
\\metroform\sp_a229
0
\\metroform\sp_a230
0
\\metroform\sp_a231
0
0
\\metroform\sp_a227
1
\\metroform\sp_a230
2
\\metroform\sp_a231
3
\\metroform\sp_a228
4
\\metroform\sp_a229
5
----- end
Here is what happens if the line is not commented out...
----- start
D:\temp>kix32 newprint
0
\\metroform\sp_a227
1
\\metroform\sp_a229
2
\\metroform\sp_a231
3
0
\\metroform\sp_a227
1
\\metroform\sp_a230
2
\\metroform\sp_a231
3
\\metroform\sp_a228
4
\\metroform\sp_a229
5
----- end