tjcarst
(Hey THIS is FUN)
2003-09-25 08:53 PM
Printer mapping - modification of Howard's script based on group membership

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

----------------


Sealeopard
(KiX Master)
2003-09-26 04:15 PM
Re: Printer mapping - modification of Howard's script based on group membership

code:
left($GrpName, 4) = " prt_"

compares a four-character string on the left side to a fice-character string on the right side.


tjcarst
(Hey THIS is FUN)
2003-09-26 08:43 PM
Re: Printer mapping - modification of Howard's script based on group membership

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.

Sealeopard
(KiX Master)
2003-09-26 08:54 PM
Re: Printer mapping - modification of Howard's script based on group membership

[Confused] A four-character string can never equal a five-charactcer string, thus your IF statement will alwasy evaluate to FALSE!

I also think that the other spaces in the first line of code are incorrect, too.


kholm
(Korg Regular)
2003-09-26 11:05 PM
Re: Printer mapping - modification of Howard's script based on group membership

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


tjcarst
(Hey THIS is FUN)
2003-10-08 10:55 PM
Re: Printer mapping - modification of Howard's script based on group membership

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 ]


Sealeopard
(KiX Master)
2003-10-09 04:22 AM
Re: Printer mapping - modification of Howard's script based on group membership

Use SPLIT to plit up your names as you like or upgrade to KiXtart v4.22RC1 which now supports negative numbers in LEFT and RIGHT.

tjcarst
(Hey THIS is FUN)
2003-10-09 04:36 AM
Re: Printer mapping - modification of Howard's script based on group membership

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



tjcarst
(Hey THIS is FUN)
2003-10-09 04:53 AM
Re: Printer mapping - modification of Howard's script based on group membership

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 ]


Sealeopard
(KiX Master)
2003-10-09 04:56 PM
Re: Printer mapping - modification of Howard's script based on group membership

See also the OSID() - Identifies operating system, service pack level, and OS kernel UDF for OS and role differentiation
code:
$os=osid()
if $os[1]='Win9x' or $os[2]<>'Workstation'
exit 0
endif



tjcarst
(Hey THIS is FUN)
2003-10-09 05:50 PM
Re: Printer mapping - modification of Howard's script based on group membership

Thanks, Jens. I discovered that just after posting. I'll give it a try.

tjcarst
(Hey THIS is FUN)
2003-10-09 11:34 PM
Re: Printer mapping - modification of Howard's script based on group membership

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?


LonkeroAdministrator
(KiX Master Guru)
2003-10-09 11:38 PM
Re: Printer mapping - modification of Howard's script based on group membership

wonder...
code:
If not instr(@producttype,'Workstation') and not
instr(@producttype,'Professional') and not
instr(@producttype,'95')
quit
endif

in english:
-if not workstation quit
-if not professional quit
-if not 95 quit

thus, this will only run on:
windows 95 professional workstation

or did I miss something?

[ 10. October 2003, 00:04: Message edited by: Lonkero ]


tjcarst
(Hey THIS is FUN)
2003-10-10 03:23 AM
Re: Printer mapping - modification of Howard's script based on group membership

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 ]


LonkeroAdministrator
(KiX Master Guru)
2003-10-10 03:25 AM
Re: Printer mapping - modification of Howard's script based on group membership

well, you have 2 ways.
either create a computer-printer-map-groups or start using that ini-file [Wink]


tjcarst
(Hey THIS is FUN)
2003-10-10 03:35 AM
Re: Printer mapping - modification of Howard's script based on group membership

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 ]


Sealeopard
(KiX Master)
2003-10-10 04:00 AM
Re: Printer mapping - modification of Howard's script based on group membership

See the UDF Forum under UnmapPrinters() - Removes mapped printers not listed in an .INI file for an example .INI file for printer mappings.

tjcarst
(Hey THIS is FUN)
2003-10-10 05:02 AM
Re: Printer mapping - modification of Howard's script based on group membership

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.


Sealeopard
(KiX Master)
2003-10-10 03:27 PM
Re: Printer mapping - modification of Howard's script based on group membership

The disadvantage of your .INI file structure is that you are wasting the section name on something unnecessary. By utilizing the section name as the first differentiator, you need to read less information in the long run.

you will first need to figure out the mapping logic, whether ti's based on the printer, group memebrship, username, and so on.

Please outline the way you decide printer mappings in detail, and I might be able to provide you with an optimized .INI file.


tjcarst
(Hey THIS is FUN)
2003-10-10 06:00 PM
Re: Printer mapping - modification of Howard's script based on group membership

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


tjcarst
(Hey THIS is FUN)
2003-10-10 06:05 PM
Re: Printer mapping - modification of Howard's script based on group membership

This kix script generates a log file, but I do not like that it generates the name of the printer as installed on the pc and not the actual \\server\share name. I could probably change this using the ini file.

</font><blockquote><font size="1" face="Verdana, Helvetica, sans-serif">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">break on

;********** Prevent logon script on server *********
call @ScriptDir+'\osid.udf'

$os=osid()
if $os[2]<>'Workstation'
exit 0
endif

;********** Beginning of printer info gathering script *********
$PrtLog="\\ps-01\Prtlog$"
$PrtLog=$PrtLog+"\@wksta"+"_"+"@userid"
;$PrtLog=$PrtLog+"\@wksta"
if 0=exist($PrtLog)
MD $PrtLog
endif
$PrtLog=$Prtlog+"\default.log"
call "@scriptdir\getdefaultprinter.udf"

$rc=Get_Default_printer()

$rc=WriteLog2($PrtLog,Get_Default_Printer(),1)

;if (RedirectOutput("$PrtLog",1) = 0)
;endif


;-------------------------------------------------------------------------------;FUNCTION WriteLog2()
;
;AUTHOR Howard A. Bullock (hbullock@tycoelectronics.com)
;
;ACTION Generic logging facility for scripts. Appends log entry to a file with an
; optional TimeStamp.
;
;SYNTAX WriteLog2($File, $text, [0|1])
;
;PARAMETERS $File (Required) - String value
; $text (Required) - String value
; $TimeStamp (Optional) Default(0) no TimeStamp (1 or 0)
;
;
;REMARKS This function writes (appends) an optionally time stamped log entry to the file
; defined in function. This function searches for the first unused file handle,
; open the file, and write the entry. The file handle is then closed. When the
; function is unable to perform its it write the error is displayed in a message box.
;
;RETURNS Nothing
;
;DEPENDENCIES None
;
;EXAMPLES WriteLog2("junk.txt","This is a test")
; WriteLog2("junk.txt","This is a test",0)
; WriteLog2("junk.txt","This is a test",1)
;
Function WriteLog2($File, $Text, optional $TimeStamp)
dim $RC, $File, $text, $FH, $TimeStamp
$FH=1
$RC=Open ($FH, $File, 5)
while $RC = -3
$FH=$FH +1
$RC=Open ($FH, $File, 5)
Loop
Select
Case $RC=0
if ($TimeStamp=1)
$TimeStamp = @Date + " " + @Time + " - "
else
$TimeStamp = ""
endif
$RC=Writeline ($FH, $TimeStamp + $Text + @CRLF)
$RC=Close ($FH)
Case $RC=-2
$text = "WriteLog2: Invalid file handle ($FH) specified when trying to Open $File."
$RC=MessageBox ($text,"Script Error",48)
Case $RC=-1
$text = "WriteLog2: Invalid file name ($File) specified for log file."
$RC=MessageBox ($text,"Script Error",48)
Case $RC=>0
$text = "System Error($RC) while attempting to open log file ($File)."
$RC=MessageBox ($text,"Script Error",48)
Endselect
EndFunction[/code]</blockquote><font size="2" face="Verdana, Helvetica, sans-serif">sample contents of default.log
-------------------------
2003/09/24 10:05:43 - Q-MIS2 IP,winspool,Ne01:
2003/09/24 10:11:09 - NONE
2003/09/24 10:13:02 - Fax,winspool,Ne00:
--------------------------


tjcarst
(Hey THIS is FUN)
2003-10-10 06:11 PM
Re: Printer mapping - modification of Howard's script based on group membership

I have the part figured out that generates the log and records time, userid and printer.

tjcarst
(Hey THIS is FUN)
2003-10-10 06:15 PM
Re: Printer mapping - modification of Howard's script based on group membership

I would also like to record if there is a local printer in the ini file. Sometimes we have local deskjets on lpt1, but the primary printer is a network laser. Other times it is th deskjet that is the default. I'd like to record both and set correct printer.

I'd prefer that the ini file is used for all printer add/deletes. If we wanted to delete a printer, do it in the ini file, not on the pc.

Here's the current printsetup.kix that generates my existing ini file.

code:
;********** Prevent logon script on server *********
call @ScriptDir+'\osid.udf'

$os=osid()
if $os[2]<>'Workstation'
exit 0
endif

;********** Prevent logon script for Health Info Mgmt *********
if InGroup ("Medrec Group")
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

Results of printsetup.kix

[Printers]
1=\\mrh-01\q-mis1
2=\\mrh-01\q-mis2
Default=\\mrh-01\q-mis2

[ 12. October 2003, 21:01: Message edited by: tjcarst ]


tjcarst
(Hey THIS is FUN)
2003-10-10 08:46 PM
Re: Printer mapping - modification of Howard's script based on group membership

sealeopard - I have a post going here where Howard is providing assistance on this issue.

http://www.kixtart.org/cgi-bin/ultimatebb.cgi?ubb=get_topic&f=14&t=001201#000022

I just posted this idea - what do you think? I'll test my script and post for you in a bit:

Okay, this may be extra work creating groups, but it may be the easiest way to set the primary printer when there are multiple printers installed.

Create a group specifically for a default printer group. Like DefPrinter_MRH-01_Q-MIS2 and put pcs in this group to get default/only printer of Q-MIS2. Create another group Printer_MRH-01_Q-MIS2 for users that have this additional printer. Use the SetDefaultPrinter in the script to set default printer if group name starts with DefPrinter.

tjcarst


tjcarst
(Hey THIS is FUN)
2003-10-10 09:02 PM
Re: Printer mapping - modification of Howard's script based on group membership

Okay here's the script.

It seems to do what I need. I have to create a group for the default printer and a group for secondary printers, but it works, even if a little cumbersome.

tjcarst

code:
;http://www.kixtart.org/board/ultimatebb.php?ubb=get_topic;f=1;t=007920

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

;********** 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
for each $grp In $WS.Groups
$GrpName = $grp.Name

;--- Add and set default printer ---

if left($GrpName,11) = "PrinterDef_"
$defprinter = substr($GrpName,12)
$defprinter = join(split($defprinter,"_"),"\")

if not PriMapState("\\" + $printer)
? "Status - Printer not connected \\"+$printer
$S=AddPrinterConnection("\\" + $printer)
? "Status - Printer added \\"+$printer
? @serror
?
$S=SetDefaultPrinter ("\\" + $defprinter)
? "Status - Default Printer set \\"+$defprinter
? @serror
endif

if PriMapState("\\" + $defprinter)<>2
$S=SetDefaultPrinter ("\\" + $defprinter)
? "Status - Default Printer set \\"+$defprinter
? @serror
endif

endif


;--- Add additional printers ---

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