#46049 - 2003-09-25 08:53 PM
Printer mapping - modification of Howard's script based on group membership
|
tjcarst
Hey THIS is FUN
Registered: 2003-09-08
Posts: 243
Loc: USA
|
I've been messing with modifying the syntax on the script from Howard below, but can't seem to get it right - I keep getting "The network path not found" errors. Any assistance is greatly appreciated.
What I have in my AD environment is the following:
Printers (over 100 with similar format Q-???): Q-ACCT Q-MIS Q-Research
Print Server: (only one PS in environment now - so not reflected in group names in AD)
MRH-01
AD Group Names containing computer accounts:
Q-ACCT Printer Q-MIS Printer
Howard's script: ----------------------------
You will need to create a group for each printer you want to map.
For example, if your printer is named " laserjet1" on the server named " server1" then create a group called " prt_server1~laserjet1" . Place the computers that should map this printer into the group.
code:
$usr = GetObject(" WinNT://" + @domain + " /" + @wksta + " $$" ) if @error <> 0 ? @serror else For Each $grp In $usr.Groups $GrpName = $grp.Name if left($GrpName, 4) = " prt_" $printer = substr($GrpName, 5) $printer = join(split($printer," ~" )," \" ) AddPrinterConnection(" \\" + $printer) endif Next endif
----------------
|
Top
|
|
|
|
#46051 - 2003-09-26 08:43 PM
Re: Printer mapping - modification of Howard's script based on group membership
|
tjcarst
Hey THIS is FUN
Registered: 2003-09-08
Posts: 243
Loc: USA
|
Thanks, sealeopard. I have that info. I am just having trouble modifying the syntax to fit my environment for printer names and group names. I was hoping for a little help in modifying that bit.
|
Top
|
|
|
|
#46053 - 2003-09-26 11:05 PM
Re: Printer mapping - modification of Howard's script based on group membership
|
kholm
Korg Regular
Registered: 2000-06-19
Posts: 714
Loc: Randers, Denmark
|
When you use Howard's script you have to obay to the naming convention he has set for the groupnames.
Using Howard's script, a printergroup must start with the letters: PRT_
You have only one printserver now, BUT if you don't include the servername in the groupname now, You could get into serious trouble, when you add another printserver to the domain, so a good advice is to use Howard's script as is.
To make the script work, asuming the name of the printserver is: MRH-01, you should rename the group: Q-ACCT Printer To: PRT_MRH-01~Q-ACCT And rename the group: Q-MIS Printer To: PRT_MRH-01~Q-MIS
Also, You have a lot of spaces in your strings, that should not be there, ie.
" /" is not the same as "/"
AND, the one Sealeopard pointed out: " prt_" is not the same as "prt_" (" prt_" is five long)
After renaming the groups, you should use this (Cleaned up for spaces) script:code:
$WS = GetObject("WinNT://" + @domain + "/" + @wksta + "$$") if @error ? @serror else For Each $grp In $WS.Groups $GrpName = $grp.Name if left($GrpName,4) = "prt_" $printer = substr($GrpName,5) $printer = join(split($printer,"~"),"\") AddPrinterConnection("\\" + $printer) endif Next endif
-Erik
|
Top
|
|
|
|
#46054 - 2003-10-08 10:55 PM
Re: Printer mapping - modification of Howard's script based on group membership
|
tjcarst
Hey THIS is FUN
Registered: 2003-09-08
Posts: 243
Loc: USA
|
Thanks, everyone for your replies.
Alas, I do not want to change my naming scheme to be similar to PRT_MRH-01~Q-ACCT. I don't like the way it appears in AD.
I prefer the group names of Q-Acct Printer as my group name, but can add the print server name to the beginning. I want to strip off the last 8 characters of the group name, get the printer name, and add it to the printserver.
I did notice the extra spaces and had already cleaned them up. I just wanted to modify the script to work with my naming scheme.
So current:
Printers (over 100 with similar format Q-???): Q-ACCT Q-MIS Q-Research
Print Server MRH-01
To account for Print Server change, AD Group Names containing computer accounts (Print Server at beginning):
MRH-01_Q-ACCT Printer MRH-01_Q-MIS Printer
I'll try to modify, but know it is wrong. Trying to get from the right side of the name is tougher than getting from the beginning as the length changes. Maybe I will have to call the groups Printer MRH-01_Q-MIS instead.
code:
$WS = GetObject("WinNT://" + @domain + "/" + @wksta + "$$") if @error ? @serror else For Each $grp In $WS.Groups $GrpName = $grp.Name if right($GrpName,7) = "Printer" $printer = substr($GrpName,len ($GrpName)-8) $printer = join(split($printer," "),"\") AddPrinterConnection("\\" + $printer) endif Next endif
[ 08. October 2003, 23:02: Message edited by: tjcarst ]
|
Top
|
|
|
|
#46056 - 2003-10-09 04:36 AM
Re: Printer mapping - modification of Howard's script based on group membership
|
tjcarst
Hey THIS is FUN
Registered: 2003-09-08
Posts: 243
Loc: USA
|
Thanks for the info, sealeopard. I will wait for final 4.22 - I don't like running anything that is RC on my network. There are just too many variables to worry about. Win9x, NT4, 2000, XP, Terminal Server, Citrix Metaframe, RAS, VPN, etc.
I'll work on this some more, it isn't currently running yet and I have no error, so need to turn the error reporting to screen.
Printer Name: Q-MIS Print Server Name: MRH-01 Computer Group Name: Printer_MRH-01_Q-MIS
(I wish I could leave a space between Printer and MRH-01_Q-MIS, but thought maybe that is why my script was failing.)
code:
$WS = GetObject("WinNT://" + @domain + "/" + @wksta + "$$")if @error ? @serrorelse For Each $grp In $WS.Groups $GrpName = $grp.Name if right($GrpName,8) = "Printer_" $printer = substr($GrpName,9) $printer = join(split($printer,"_"),"\") AddPrinterConnection("\\" + $printer) endif Next endif
|
Top
|
|
|
|
#46057 - 2003-10-09 04:53 AM
Re: Printer mapping - modification of Howard's script based on group membership
|
tjcarst
Hey THIS is FUN
Registered: 2003-09-08
Posts: 243
Loc: USA
|
I eventually want to get it to the point that it does not run on a server and Win95 (setdefaltprinter and addprinter do not run). I also want it to check the current state of the printer and map if not map, and set default if already mapped. Do nothing if mapped and default.
So, using the previous posted script that I still am working on:
code:
;********** Prevent script from running on servers and Win95 ********* If not instr(@producttype,'Workstation') and not instr(@producttype,'Professional') and not instr(@producttype,'95') quit endif
;********** Beginning of printer mapping *********
$WS = GetObject("WinNT://" + @domain + "/" + @wksta + "$$") if @error ? @serror else for each $grp In $WS.Groups $GrpName = $grp.Name if left($GrpName,8) = "Printer_" $printer = substr($GrpName,9) $printer = join(split($printer,"_"),"\")
if not PriMapState("\\" + $printer") ? "Status - Printer not connected" ? @serror $S=AddPrinterConnection("\\" + $printer) endif
if PriMapState("\\" + $printer) $S=SetDefaultPrinter ("\\" + $printer) ? "Status - Default Printer set" ? @serror endif endif next endif
;********** Beginning of Function ********* ;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
[ 09. October 2003, 05:07: Message edited by: tjcarst ]
|
Top
|
|
|
|
#46059 - 2003-10-09 05:50 PM
Re: Printer mapping - modification of Howard's script based on group membership
|
tjcarst
Hey THIS is FUN
Registered: 2003-09-08
Posts: 243
Loc: USA
|
Thanks, Jens. I discovered that just after posting. I'll give it a try.
|
Top
|
|
|
|
#46060 - 2003-10-09 11:34 PM
Re: Printer mapping - modification of Howard's script based on group membership
|
tjcarst
Hey THIS is FUN
Registered: 2003-09-08
Posts: 243
Loc: USA
|
I have the script adding printers, but it isn't using the PriMapState correctly. It adds three printers if they don't exist, but adds one all of the time, not trying to add the other two.
I would like to use @primarygroup to use to set default printer, but am not sure how to modify this.
I am at the point where I think an INI file may have been simpler. I already have capture INI files to a network drive so that I could determine which groups to put the computers into.
Any ideas on using @primarygroup with PriMapState.udf to set default printer?
|
Top
|
|
|
|
#46062 - 2003-10-10 03:23 AM
Re: Printer mapping - modification of Howard's script based on group membership
|
tjcarst
Hey THIS is FUN
Registered: 2003-09-08
Posts: 243
Loc: USA
|
No, you didn't miss anything.
I've since changed it as sealeopard suggested to use OSID.UDF.
But, I found a mistake in my code that was causing the one printer to repeatedly add. Now the script is adding the printers correctly based on computer group membership.
However, many computers have more than one printer, and I am having trouble setting the default one. The script uses computer group membership, and @primarygroup uses user group membership. If I could use the computer group membership and read the primary group of the computer to set the printer, I'd have things working.
I am beginning to think I should have done this using an .ini file. I have the files for all pc's already, I collected the currently installed printers and the default printer for each workstation+username combo. I could probably, after weeks of messing with the script, read the ini file and write the new printers to the pc's if they did not already exist.
Until just recently, I've never messed with Kix, and have no programming background beyond basic batch files, ini files, registry hacks, and a little html code. Nothing along the logic of programming. So, I am struggling and often do things that make no sense to you guys, rightly so.
I find it fascinating and continue to mess with it at home during non-work hours so I don't catch hell, but can't help but try to get this to work. I'm persistent and stubborn, so eventually, I'll get it. You guys have been wonderful with your patience and assistance. I apologize for asking what must be obvious to the experienced Kix programmer.
Currently I have the following script that adds the printers, but haven't figured out how to set default based on computer primary group membership:
Printer: Q-MIS Server: MRH-01 Group: Printer_MRH-01_Q-MIS
code:
call @ScriptDir+'\PriMapState.udf'
;********** Prevent script from running 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
;********** Beginning of printer mapping ********* $WS = GetObject("WinNT://" + @domain + "/" + @wksta + "$$") if @error ? @serror else for each $grp In $WS.Groups $GrpName = $grp.Name
if left($GrpName,8) = "Printer_" $printer = substr($GrpName,9) $printer = join(split($printer,"_"),"\")
if not PriMapState("\\" + $printer) ? "Status - Printer not connected "+$printer $S=AddPrinterConnection("\\" + $printer) ? "Status - Printer added "+$printer ? @serror ? endif endif next endif
edited to correct code [ 10. October 2003, 03:26: Message edited by: tjcarst ]
|
Top
|
|
|
|
#46064 - 2003-10-10 03:35 AM
Re: Printer mapping - modification of Howard's script based on group membership
|
tjcarst
Hey THIS is FUN
Registered: 2003-09-08
Posts: 243
Loc: USA
|
I'd better start on the ini file. Here's what I have pilfered from others here at this site (Thanks!). Currently I am just reading the printers and creating the ini file. Before posting, I uncommented the part that writes the printers, mainly to let you know I had some code already, but wasn't brave enought to start using. I didn't want you to spend time trying to help me out with code you've probably already posted here. If you have any suggestions on the script, they are valued.
Thanks.
Current Printersetup.ini file
[Printers] 1=\\mrh1\q-mis2 2=\\mrh-01\q-mis2-color 3=\\mrh-01\q-mis2 Default=\\mrh-01\q-mis2
Current Script that writes Printersetup.ini
code:
;********** Prevent logon script on server ********* call @ScriptDir+'\osid.udf'
$os=osid() if $os[2]<>'Workstation' exit 0 endif
;********** Beginning of printer info gathering script *********
;Re-Write at http://www.kixtart.org/cgi-bin/ultimatebb.cgi?ubb=get_topic;f=2;t=004308 ;By Kdyer & Lonkero (15th april 2003)
;Re-Write of http://www.kixtart.org/board/ultimatebb.php?ubb=get_topic;f=10;t=000021
;Default Printer Configuration ;This is to capture the Default Printer and get all network defined printers as well ; Ref. http://www.kixtart.org/cgi-bin/ultimatebb.cgi?ubb=get_topic&f=1&t=006790 ; Ref. http://www.kixtart.org/board/ultimatebb.php?ubb=get_topic;f=12;t=000221 $loc='HKEY_CURRENT_USER\Network\LPT1' $reg='HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion' $dev=$reg+'\Devices' $nprt=split(READVALUE($reg+'\Windows','Device'),',')[0] $sysdrv=SUBSTR('%WINDIR%',1,2) dim $priReg, $x, $i, $rc, $ps ; Set Printsetup.ini file location $ps=('\\mrh-01\prtlog$') $ps=($ps+'\@wksta'+'_'+'@userid') ;$ps=($ps+'\@wksta') IF 0=EXIST($ps) MD $ps ENDIF $ps=$ps+'\Printsetup.ini' $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 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
[ 10. October 2003, 03:41: Message edited by: tjcarst ]
|
Top
|
|
|
|
#46066 - 2003-10-10 05:02 AM
Re: Printer mapping - modification of Howard's script based on group membership
|
tjcarst
Hey THIS is FUN
Registered: 2003-09-08
Posts: 243
Loc: USA
|
Thanks, sealeopard.
This ini file has more info than what I need. Currently, when a user logs in, a subdirectory is created (if it does not exist) in the format of @wsksta+@userid and then writes the current and default printers.
I have over 1200 users and most have logged on and created these files. Over 100 printers. I have only 550 pcs, so I may consolidate the folders by eliminating the usernames and having all users use just the folder name of the computer. There are some users that don't want to use the same printers, which is why I set it up for each user and workstation. Some users/pc's only have one printer, others have over a dozen. Very messy.
Creating a more complex ini may make sense had I not already created the simple ones.
Maybe I am misunderstanding your suggestion? Is it not okay to use the script and existing ini format? Or am I asking for trouble down the road?
Thanks for your patience. I know I am testing it.
|
Top
|
|
|
|
#46068 - 2003-10-10 06:00 PM
Re: Printer mapping - modification of Howard's script based on group membership
|
tjcarst
Hey THIS is FUN
Registered: 2003-09-08
Posts: 243
Loc: USA
|
Thanks, sealeopard.
I want to map the printers based on group membership of the COMPUTER, but cannot figure out how to set the default printer based on the computer due to the RID/SID number and not name that is generated by @primarygroup.
What will work, is generating an ini file for the computer when a user logs on and recording the current and default printers. If the printers are not on the pc, install them based on the ini file that exists. It would be even grander if I could add printers in this manner, but currently, if I delete a printer from my pc and logon, it just updates the ini file with the shorter list of printers. It only creates the printers if I delete all of them. It would be nice to also create a log file that generates a time stamp with the current printers and the username that created the timestamp.
I need to exclude terminal server users and servers from running this script, I have this part figured out.
tjcarst
|
Top
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
0 registered
and 331 anonymous users online.
|
|
|