TITLE: Default Printer Configuration
AUTHOR: Kent Dyer
VERSION: Version 1.17
DATE: Updated 15 May 2009
DESCRIPTION:
This sets up printers and captures the local Printer mapped via the network and writes to an INI file. The reason for the INI is that the computer system may change location or user gets a new imaged desktop
Also, if a new person logs in under a new profile/system, they will get the same printer.

Code tries to "favor" the Locally installed printer.

This was written in frustration with Microsoft Article: 252388 (formerly: Q252388)
Reference: How to Add a Default Printer for All New Users
Comments and feedback are welcome to - leptonator@hotmail.com
Code was inspired from - Koppelen van de defaultprinter aan een LPT-poort (voor DOS programma's) (no, I don't know Dutch <img src="/ubbthreads/images/graemlins/smile.gif" alt="" /> ) which was found at http://www.robvanderwoude.com/files/printers.txt
CHANGE LOG:
  • 15 March 2009 - 1.17 - No code changed: Update e-mail address.
  • 18 March 2005 - 1.17 - Fix: FMF Form Generator and if ini file exists, but not of correct format.
  • 17 March 2005 - 1.16 - Fix: FMF Form Generator, Added support for: Microsoft Office Document Image Writer
  • 9 February 2005 - 1.15 - Added in detection for FMF Generator for Microsoft Office
  • 15 April 2003 - 1.14 - Code clean-up - Thanks Lonkero!
  • 14 April 2003 - 1.13 - Code Re-write - now captures all network printers - Thanks Lonkero!
  • 10 April 2003 - 1.12 - Fixed issue with reading in Printer
  • 26 March 2003 - 1.11 - Fixed issue with reading in Printer
  • 26 March 2003 - 1.11 - Added code to delete LPT1 on a network printer if a local is installed
  • 26 March 2003 - 1.11 - Added code to "favor" local printer if Network printer changes
  • 22 March 2003 - 1.10 - Trimmed down the Network Printer Setup
  • 28 February 2003 - 1.09 - Cleaned up code, Check for local default Printer did not work properly, version table updated
  • 15 January 2003 - 1.08 - Corrected the USE LPT1: /DELETE /PERSISTENT (Kixtart 4.20 Testing found this)
  • 15 January 2003 - 1.07 - Added in a check to insure that the network printer was set as the default.
  • 9 December 2002 - 1.06 - Added both HKEY_LOCAL_MACHINE and HKEY_CURRENT_USER
  • 5 December 2002 - 1.05 - Changed Script to favor locally installed printer rather than the INI file
  • 21 November 2002 - 1.04 - Changed title from Default Printer to Default Printer Configuration
  • 21 November 2002 - 1.04 - Added in a section (below) for Local Printer Thanks "jojopuppyfish"
  • Ref. RFC - Clean-up of FAQ Post for Printer Setup/Capture
  • 21 November 2002 - 1.04 - Simplified and cleaned up code!
  • 21 November 2002 - 1.04 - Removed Debug Flags, combined KiX functions
  • 21 November 2002 - 1.04 - OS Check removed as ADDPRINTERCONNECTION and SETDEFAULTPRINTER are only supported on NT
  • 8 August 2002 - 1.03 - Fixed USE Commmand - Doh!
  • 13 March 2002 - 1.02 - Added in debug flag. Thanks MCA!
  • 25 February 2002 - 1.01 - Updated for the OS check
  • 15 August 2001 - 1.00 - Originally coded

 Code:
 ; -- INITIATE THE FUNCTION..
 DEFPRTR

 FUNCTION DEFPRTR()
    ; Re-Write at http://www.kixtart.org/cgi-bin/ultimatebb.cgi?ubb=get_topic;f=2;t=004308
    ; By Kdyer &amp; 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&amp;f=1&amp;t=006790
    ; Ref. http://www.kixtart.org/board/ultimatebb.php?ubb=get_topic;f=12;t=000221
    DIM $prireg,$x,$i,$rc,$ps,$reg,$dev,$nprt,$key
    SELECT
    CASE
       @wksta='MAILROOM1' OR @wksta='MAILROOM2' ; -- MAIL STATION PCS
       RETURN
    CASE
       INSTR(@wksta,'TRAIN') ; -- TRAINING SYSTEMS
       USE LPT1: /delete
       $rc=ADDPRINTERCONNECTION('\\PSERVER\PRINTER-P18')
       SLEEP '0.5'
       $rc=SETDEFAULTPRINTER('\\PSERVER\PRINTER-P18')
       SLEEP '0.5'
       USE LPT1: '\\PSERVER\PRINTER-P18'
       RETURN
    ENDSELECT
    $reg='HKCU\Software\Microsoft\Windows NT\CurrentVersion\'
    $dev=$reg+'Devices'
    $nprt=split(READVALUE($reg+'Windows','Device'),',')[0]
    IF EXIST('H:')
       $ps='H:\Scripts_DO_NOT_DELETE'
    ELSE
       $ps=LEFT('%WINDIR%',2)+'\Scripts'
    ENDIF
    IF 0=EXIST($ps)
       MD $ps
    ENDIF
    SELECT
    CASE
       Ltrim(Split(@ipaddress0,'.')[0])+'.'+Ltrim(Split(@ipaddress0,'.')[1])='1.1'
       $ps=$ps+'\SITE1.ini'
    CASE
       Ltrim(Split(@ipaddress0,'.')[0])+'.'+Ltrim(Split(@ipaddress0,'.')[1])='2.2'
       $ps=$ps+'\SITE2.ini'
    CASE
       1
       $ps=$ps+'\PRINTSETUP.ini'
    ENDSELECT

    $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
    CASE
       $nprt='' AND EXIST($ps) ;NO DEFAULT PRINTER, LOAD PRINTERS FROM SAVED INFO
       FOR EACH $key IN split(readprofilestring($ps,'Printers',''),chr(10))
          $rc=addprinterconnection(readprofilestring($ps,'Printers',$key))
       NEXT
       $nprt=READPROFILESTRING($ps,'Printers','Default')
       $rc=SETDEFAULTPRINTER($nprt)
       USE LPT1: $nprt
    CASE
       (INSTR($nprt,'Generator') OR INSTR($nprt,'Image Writer')) AND EXIST($ps) ;MS-OFFICE PRINTER(S), LOAD PRINTERS FROM SAVED INFO
       FOR EACH $key IN SPLIT(READPROFILESTRING($ps,'Printers',''),chr(10))
          $rc=ADDPRINTERCONNECTION(READPROFILESTRING($ps,'Printers',$key))
          $rc=SETDEFAULTPRINTER(READPROFILESTRING($ps,'Printers',$key))
          ; -- Re-Read the Registry Key
          $nprt=split(READVALUE($reg+'Windows','Device'),',')[0]
          $rc=WRITEPROFILESTRING($ps,'Printers','Default',$nprt)
          IF LEFT(READPROFILESTRING($ps,'Printers',$key),2)='\\'
             USE LPT1: READPROFILESTRING($ps,'Printers','Default')
          ENDIF
       NEXT
    CASE
       NOT INSTR($nprt,'\\') ;LOCAL PRINTER DEFAULT, SO WE WANT TO REMOVE NETWORK MAPPING
       USE LPT1: /delete /persistent
    ENDSELECT
 ENDFUNCTION



Edited by Kdyer (2009-05-15 08:49 PM)
_________________________
Utilize these resources:
UDFs (Full List)
KiXtart FAQ & How to's