#196405 - 2009-10-19 10:41 PM
Get local default printer, then map network printers, leave default setting
|
sryan
Just in Town
Registered: 2009-10-19
Posts: 2
Loc: intarweb
|
I'm using Kixtart v 4.60 to map the user's DFS network shares, and shared network printers off of a Windows 2003 Server.
**The issue is that when the user logs in, the script moves the default printer setting from what they set previously to one of the network printers that they received as a result of their group Active Directory membership.
I'm looking for the Script to do the following:
Upon logon - Check the default Setting of a Local Printer - Save it to memory.
Map all available network printers according to their group but do not set the default printer to any shared network printer if a local one has already been defined previously.
Here is what I have currently
Using Kixtart v 4.60.0.0
;DFS Network Drive Mappings
If InGroup("Domain Users")
Use * /delete /persistent
Use z: "\\domain.local\Shared$\Users\%username%" /persistent
EndIf
If InGroup("Executive")
Use j: "\\domain.local\Shared$\Executive" /persistent
EndIf
If InGroup("Operations")
Use k: "\\domain.local\Shared$\Operations" /persistent
EndIf
If InGroup("Finance")
Use l: "\\domain.local\Shared$\Finance" /persistent
EndIf
If InGroup("Technology")
Use m: "\\domain.local\Shared$\Technology" /persistent
EndIf
If InGroup("Sales")
Use n: "\\domain.local\Shared$\Sales" /persistent
EndIf
If InGroup("Editorial")
Use o: "\\domain.local\Shared$\Editorial" /persistent
EndIf
If InGroup("Marketing")
Use p: "\\domain.local\Shared$\Marketing" /persistent
EndIf
:Exit
;Delete all Network Printers Only -- Local Printers will not be deleted
$key = "HKEY_CURRENT_USER\Printers\Connections"
$printers = ArrayEnumKey($key)
For Each $printer in $printers
$rc = DelKey($key + "\" + $printer)
Next
Function arrayenumkey($regsubkey)
Dim $retcode, $subkeycounter, $currentsubkey, $subkeyarray
If Not KeyExist($regsubkey)
Exit 87
EndIf
$subkeycounter=0
Do
$currentsubkey=EnumKey($regsubkey,$subkeycounter)
If Not @ERROR
ReDim preserve $subkeyarray[$subkeycounter]
$subkeyarray[$subkeycounter]=$currentsubkey
$subkeycounter=$subkeycounter+1
EndIf
Until @ERROR
$arrayenumkey=$subkeyarray
Exit 0
EndFunction
:Exit
;Map and Set Printer per Room #
If InGroup("P1005") = 1 "Mapping Network Printers"
addprinterconnection("\\dc01\Printer 1005")
setDefaultPrinter("\\dc01\Printer 1005")
EndIf
If InGroup("P1013") = 1 "Mapping Network Printers"
addprinterconnection("\\dc01\Printer 1013")
setDefaultPrinter("\\dc01\Printer 1013")
EndIf
If InGroup("P1021") = 1 "Mapping Network Printers"
addprinterconnection("\\dc01\Printer 1021")
setDefaultPrinter("\\dc01\Printer 1021")
EndIf
If InGroup("P1029") = 1 "Mapping Network Printers"
addprinterconnection("\\dc01\Printer 1029")
setDefaultPrinter("\\dc01\Printer 1029")
EndIf
If InGroup("P1031") = 1 "Mapping Network Printers"
addprinterconnection("\\dc01\Printer 1031")
SetDefaultPrinter("\\dc01\Printer 1031")
EndIf
:Exit
; All Printers for all rooms
If InGroup("All Printers") = 1 "Mapping Network Printers"
addprinterconnection("\\dc01\Printer 1005")
addprinterconnection("\\dc01\Printer 1013")
addprinterconnection("\\dc01\Printer 1021")
addprinterconnection("\\dc01\Printer 1029")
addprinterconnection("\\dc01\Printer 1031")
EndIf
:Exit
Thanks in advance.
Edited by sryan (2009-10-20 02:55 AM) Edit Reason: I had 2.60 for my version of kixtart when it should have been 4.60
|
|
Top
|
|
|
|
#196406 - 2009-10-19 11:15 PM
Re: Get local default printer, then map network printers, leave default setting
[Re: sryan]
|
Allen
KiX Supporter
   
Registered: 2003-04-19
Posts: 4567
Loc: USA
|
This is a way using GetDefaultPrinter() - http://www.kixtart.org/forums/ubbthreads.php?ubb=showflat&Number=118766 and Printerlist() - http://www.kixtart.org/forums/ubbthreads.php?ubb=showflat&Number=84385
Be sure to copy the UDFs referenced above to the bottom of your script. If you are in fact using a 2.x version of kixtart, I'd suggest moving to 4.53 or the latest 4.61.
for each $printer in printerlist(,2)
if $printer=getdefaultprinter()
$localdefault=1
endif
next
If InGroup("P1005") = 1 "Mapping Network Printers"
$RC=addprinterconnection("\\dc01\Printer 1005")
if $localdefault<>1
$RC=setDefaultPrinter("\\dc01\Printer 1005")
endif
EndIf
|
|
Top
|
|
|
|
#196407 - 2009-10-19 11:27 PM
Re: Get local default printer, then map network printers, leave default setting
[Re: Allen]
|
Allen
KiX Supporter
   
Registered: 2003-04-19
Posts: 4567
Loc: USA
|
hmmm... just thinking. Good and bad here. Good that it wont switch the local printer if it is default, but bad that it wont change the printer to the network if say something like Adobe or Microsoft XPS is the default. I suppose you could account for those type printers by:
for each $printer in printerlist(,2)
if $printer=getdefaultprinter()
if instr($printer,"Microsoft") or instr($printer,"Adobe")
$localdefault=0
else
$localdefault=1
endif
endif
next
|
|
Top
|
|
|
|
#196411 - 2009-10-20 02:13 PM
Re: Get local default printer, then map network printers, leave default setting
[Re: Allen]
|
sryan
Just in Town
Registered: 2009-10-19
Posts: 2
Loc: intarweb
|
So far this works. We'll be testing it over the next few days. Thanks again for your help.
;DFS Network Drive Mappings
If InGroup("Domain Users")
Use * /delete /persistent
Use z: "\\domain.local\Shared$\Users\%username%" /persistent
EndIf
If InGroup("Executive")
Use j: "\\domain.local\Shared$\Executive" /persistent
EndIf
If InGroup("Operations")
Use k: "\\domain.local\Shared$\Operations" /persistent
EndIf
If InGroup("Finance")
Use l: "\\domain.local\Shared$\Finance" /persistent
EndIf
If InGroup("Technology")
Use m: "\\domain.local\Shared$\Technology" /persistent
EndIf
If InGroup("Sales")
Use n: "\\domain.local\Shared$\Sales" /persistent
EndIf
If InGroup("Editorial")
Use o: "\\domain.local\Shared$\Editorial" /persistent
EndIf
If InGroup("Marketing")
Use p: "\\domain.local\Shared$\Marketing" /persistent
EndIf
:Exit
;Delete all Network Printers Only -- Local Printers will not be deleted
$key = "HKEY_CURRENT_USER\Printers\Connections"
$printers = ArrayEnumKey($key)
For Each $printer in $printers
$rc = DelKey($key + "\" + $printer)
Next
Function arrayenumkey($regsubkey)
Dim $retcode, $subkeycounter, $currentsubkey, $subkeyarray
If Not KeyExist($regsubkey)
Exit 87
EndIf
$subkeycounter=0
Do
$currentsubkey=EnumKey($regsubkey,$subkeycounter)
If Not @ERROR
ReDim preserve $subkeyarray[$subkeycounter]
$subkeyarray[$subkeycounter]=$currentsubkey
$subkeycounter=$subkeycounter+1
EndIf
Until @ERROR
$arrayenumkey=$subkeyarray
Exit 0
EndFunction
:Exit
;Get Default Printer
function GetDefaultPrinter()
$GetDefaultPrinter = join(split(readvalue("HKEY_USERS\"+@sid+"\Software\Microsoft\Windows NT\CurrentVersion\Windows","Device"),',',1),'')
endfunction
; Make Printer List
Function PrinterList(optional $remotepc, optional $displaymode)
dim $service,$printer,$printers,$printerdesc[0],$counter,$portname,$printername
if $remotepc=""
$remotepc="."
endif
$Service = GetObject("winmgmts:\\" + $remotepc + "\root\cimv2")
if @error
exit @error
endif
$Printers=$service.execquery ('select * from Win32_Printer')
for each $printer in $printers
redim preserve $printerdesc[$counter]
if $displaymode & 1
$portname = "," + $printer.portname
endif
select
case $displaymode & 4 ;remote printers
if left($printer.portname,2)="\\" or left($printer.name,2)=="\\"
$printername=$printer.name
endif
case $displaymode & 2 ;local printers
if left($printer.portname,2)<>"\\" and left($printer.name,2)<>"\\"
$printername=$printer.name
endif
case 1 ; all printers
$printername=$printer.name
endselect
if $printername<>""
$printerdesc[$counter]=$printername + $portname
$counter=$counter + 1
$printername=""
endif
next
$PrinterList=$printerdesc
endfunction
for each $printer in printerlist(,2)
if $printer=getdefaultprinter()
$localdefault=1
endif
next
;Map and Set Printer per Room #
If InGroup("P1005") = 1 "Mapping Network Printers"
$RC=addprinterconnection("\\dc01\Printer 1005")
if $localdefault<>1
$RC=SetDefaultPrinter("\\dc01\Printer 1005")
endif
EndIf
If InGroup("P1013") = 1 "Mapping Network Printers"
$RC=addprinterconnection("\\dc01\Printer 1013")
if $localdefault<>1
$RC=SetDefaultPrinter("\\dc01\Printer 1013")
endif
EndIf
If InGroup("P1021") = 1 "Mapping Network Printers"
$RC=addprinterconnection("\\dc01\Printer 1021")
if $localdefault<>1
$RC=SetDefaultPrinter("\\dc01\Printer 1021")
endif
EndIf
If InGroup("P1029") = 1 "Mapping Network Printers"
$RC=addprinterconnection("\\dc01\Printer 1029")
if $localdefault<>1
$RC=SetDefaultPrinter("\\dc01\Printer 1029")
endif
EndIf
If InGroup("P1031") = 1 "Mapping Network Printers"
$RC=addprinterconnection("\\dc01\Printer 1031")
if $localdefault<>1
$RC=SetDefaultPrinter("\\dc01\Printer 1031")
endif
EndIf
:Exit
; All Printers for all rooms
If InGroup("All Printers") = 1 "Mapping Network Printers"
$RC=addprinterconnection("\\dc01\Printer 1005")
$RC=addprinterconnection("\\dc01\Printer 1013")
$RC=addprinterconnection("\\dc01\Printer 1021")
$RC=addprinterconnection("\\dc01\Printer 1029")
$RC=addprinterconnection("\\dc01\Printer 1031")
EndIf
:Exit
Edited by sryan (2009-10-20 10:40 PM)
|
|
Top
|
|
|
|
#196412 - 2009-10-21 07:55 AM
Re: Get local default printer, then map network printers, leave default setting
[Re: sryan]
|
Allen
KiX Supporter
   
Registered: 2003-04-19
Posts: 4567
Loc: USA
|
A couple of things... Please do not edit your posts with completely different information. I had already read your post earlier today, and just happened to reread the thread and noticed your changes.
The User Defined Functions (UDFs) should be either at the top or the bottom of your script. I think most here put them at the bottom. While putting them in the middle, like you have, does work, its one of those syntax things that will help you and others down the road. Here's a FAQ that might help too. http://www.kixtart.org/forums/ubbthreads.php?ubb=showflat&Number=81594&site_id=1#import
|
|
Top
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
1 registered
(Allen)
and 675 anonymous users online.
|
|
|