PriMapState returns incorrect value if based upon registry entry for default printer.
Script I am running:
code:
;********** Prevent script from running for administrator on a server or Win95*********
if @userid = administrator
call @Scriptdir+'\osid.udf'
$os=osid()
if $os[1]='Win95' or $os[2]<>'Workstation'
exit 0
endif
endif
;********** Prevent script from running on Medrec pcs *********
if InGroup ("Medrec Group")
exit 0
endif
;********** Beginning of printer mapping *********
$WS = GetObject("WinNT://" + @domain + "/" + @wksta + "$$")
if @error
? @serror
else
$ps1="mrh-01"
for each $grp In $WS.Groups
$GrpName = $grp.Name
;--- Add additional printers ---
if left($GrpName,4) = "A01_"
$addlprinter = substr($GrpName,5)
$addlprinter = $ps1+"\"+$addlprinter
$addlprinter = "\\"+$addlprinter
? "Additional Printer(s): "$addlprinter
if not PriMapState($addlprinter)
? "Status: Printer not connected " + $addlprinter
$S=AddPrinterConnection($addlprinter)
? "Status: Printer added " + $addlprinter
? @serror ?
endif
endif
; next
;--- Add and set default printer ---
if left($GrpName,4) = "D01_"
$defprinter = substr($GrpName,5)
$defprinter = $ps1+"\"+$defprinter
$defprinter = "\\" + $defprinter
? "Default Printer from computer group membership: "$defprinter
? $def=readvalue("HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows","Device")
? "Default printer in registry: "$def
?
if not PriMapState($defprinter)
? "Status - Printer not connected "+$defprinter
$nul=AddPrinterConnection($defprinter)
? "Status: Printer added "+ $defprinter
? @serror ?
$nul=SetDefaultPrinter($defprinter)
? "Status: Default Printer set " +$defprinter
? @serror ?
endif
$rc=PriMapState($defprinter) ; if PriMapState($defprinter)<>2
? "Value returned by PriMapState: "$rc
?
if $rc=1
$nul=SetDefaultPrinter($defprinter)
? "Status: Default Printer set " + $defprinter
? @serror ?
endif ; endif
endif
next
;end
;FUNCTION PriMapState v1.1
;
;AUTHOR Lonkero (Jooel.Nieminen@gwspikval.com)
;
;ACTION Checks for existent networkprinter connection
;
;SYNTAX PriMapState(PRINTER)
;
;PARAMETERS PRINTER
; Printers name to be checked
;
;RETURNS 1 if printer connected
; 2 if printer is default
; nothing if not connected
;
;REMARKS code for w9x adapted from BrianTX
;
;DEPENDENCIES none
;
;EXAMPLE if not PriMapState('\\server\printer1')
; "printer not connected!"
; endif
;
;CODE
function PriMapState($_Pri)
if @inwin=1
if len(readvalue("HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Devices",$_Pri))
if instr(readvalue("HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows","Device"),$_Pri)
$PriMapState=2
else
$PriMapState=1
endif
endif
else
dim $_Root,$_C,$_C2 $_Root="HKLM\System\CurrentControlSet\control\Print\Printers"
for $_C=0 to 259
$_C2=enumkey($_Root,$_C)
If instr(READVALUE($_Root+"\"+$_C2,"Port"),$_Pri)
If instr(READPROFILESTRING("%windir%\win.ini","windows","device"),$_Pri)
$PriMapState = 2
Else
$PriMapState = 1
Endif
Endif
if $_C2=259 $_C=$_C2 endif
next
endif
endfunction
Results of above script at command prompt:
----------------------------------
W:\>kix32 printers
Default Printer from computer group membership: \\mrh-01\q-mis2
Default printer in registry: \\mrh-01\q-mis2-color,winspool,Ne03:
Value returned by PriMapState: 2
Additional Printer(s): \\mrh-01\q-research
Additional Printer(s): \\mrh-01\q-research-color
Additional Printer(s): \\mrh-01\q-mis1
Additional Printer(s): \\mrh-01\q-mis2-color
W:\>
----------------------------------
tjcarst