Hi Guys,
I've finally got around to tidying up our logon script at work - its always just had something bodged onto it when needed.
The basic rewrite includes these two sections that read from an ini file that's been copied to the users temp folder. The variable $logonini=@site+".ini" for most users, I've forced it to KENT.ini for the time being.
The printers section works great and is included to show what I want to happen. The drive mappings section is where the problems lie:
Code:
$debug=1
$dodrives=1
$doprinters=1
$title="Logon Script"
$logonini="KENT.ini"
if $debug
$ret=messagebox("Site detected as "+@site+".",$title)
endif
;; Map the network drives
if $debug
$ret=messagebox("Map network drives",$title)
endif
if $dodrives
$strings=""
$section="MAPPINGS"
$strings=ReadProfileString("%temp%\$logonini",$section,"")
if $debug messagebox($strings,$title) endif
$mappingarray=split($strings, chr(10))
;if $debug messagebox($mappingarray,$title) endif ;; For Each $driveletter IN $mappingrarray
if $debug messagebox($driveletter,$title+" 2") endif ;; $driveshare=(ReadProfileString("%temp%\$logonini",$section,$driveletter))
if $debug messagebox("Mapping "+$driveletter+" to "+$driveshare,$title) endif
Use $driveletter $driveshare
Next
endif
;; Printers
if $doprinters
;; Read current default printer
$DefaultPrinter = ""
$RegKey = "HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows"
$KeyValue = ReadValue ($RegKey, "Device")
If @ERROR = 0
$DefaultPrinter = SubStr($KeyValue, 1, InStr($KeyValue, ",") - 1)
EndIf
if $debug
messagebox("Default printer is "+$DefaultPrinter,$title)
endif
;; Remove obsolete printers
$strings=ReadProfileString ("%temp%\$logonini","PRINTERDEL","")
$printerarray=split($strings, chr(10))
For Each $printer IN $printerarray
$printername=(ReadProfileString("%temp%\$logonini","PRINTERDEL",$printer))
if $debug
messagebox("Deleting "+$printer,$title)
endif
DelPrinterConnection($printername)
Next
;; Add new printers
$delbeforeadd=1
$delbeforeadd=ReadProfileString("%temp%\$logonini","MISC","delprnbeforeadd")
$strings=""
$strings=ReadProfileString ("%temp%\$logonini","PRINTERS","")
$printerarray=split($strings, chr(10))
For Each $printer IN $printerarray
if $debug
messagebox("Adding "+$printer,$title)
endif
$printername=(ReadProfileString("%temp%\$logonini","PRINTERDEL",$printer))
if $delbeforeadd
DelPrinterConnection($printername)
endif
AddPrinterConnection($printername)
Next
;; Reset Default printer back to the one we had at the beginning!
SetDefaultPrinter($DefaultPrinter)
endif
The INI files contain the following:
KENT.ini
Code:
[MAPPINGS]
J:=\\knt-srv-001\projects
K:=\\knt-srv-001\admin
L:=\\knt-srv-001\Archiving
M:=\\knt-srv-001\general
S:=\\knt-srv-001\drivers
[PRINTERS]
printer1=\\knt-srv-003\Kyocera
printer2=\\knt-srv-003\HPDesign
printer3=\\knt-srv-003\KyoceraF
printer4=\\knt-srv-003\Kyocera3
printer5=\\knt-srv-003\HP5500
printer6=\\knt-srv-001\PDFCreat
[PRINTERDEL]
printer1=\\knt-srv-001\Kyocera
printer2=\\knt-srv-001\HPDesign
printer3=\\knt-srv-001\KyoceraF
printer4=\\knt-srv-001\Kyocera3
printer5=\\knt-srv-001\HP5500
Finally, I have an old bit of code which did it completely differently, which is listed below.
Code:
$ret=RedirectOutput ("%temp%\pba.tmp", 1)
ReadProfileString ("%temp%\$logonini","MAPPINGS","")
$ret=RedirectOutput ("")
$ret=Open(1,"%temp%\pba.tmp",2)
$letter=SubStr (ReadLine(1),1,2)
While $letter
Use $letter ReadProfileString("%temp%\$logonini","MAPPINGS",$letter)
$letter=SubStr (ReadLine(1),1,2)
Loop
$ret=Close(1)
Any suggestions or reasons why the 'nice' code doesn't do it and the hacky bit does would be gratefully received!
TIA
David.