#71502 - 2002-11-09 06:02 AM
RFC - Clean-up of FAQ Post for Printer Setup/Capture
|
Kdyer
KiX Supporter
   
Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
|
Ref. FAQ - Default Printer
I am interested in cleaning up the following script..
It seems to work great..
- What this does is to read in the default printer from the registry.
- Checks to see if it is not Adobe Writer, Winfax, etc. exists. If so, it leaves.
- If no printer is found, there is a check made for the INI file.
- If the INI file is found, ADDPRINTERCONNECTION and SETDEFAULTPRINTER are initiated.
- If the printer already exists, it skips the ADDPRINTERCONNECTION and SETDEFAULTPRINTER.
- Once this done, it captures the printer to the INI file.
- Lastly, a USE to LPT1 is done as we are doing more with Citrix.
code:
; -- Printer Configuration ; -- Kent Dyer ; -- Updated 8 August 2002 ; -- Originally coded 15 August 2001 ; -- 8 August 2002 - Fixed USE Commmand - Doh! ; -- 13 March 2002 - Added in debug flag. Thanks MCA! ; -- Updated on 25 February 2002 for the OS check ; -- This sets up for printers ; -- Captures the local Printer and writes to an INI file ; -- The reason for the INI is that the computer system may change location ; -- Also, if a new person logs in under a new profile, they will get the same printer ; -- This was written in frustration with Q252388 ; -- http://support.microsoft.com/default.aspx?scid=kb;EN-US;q252388 ; -- Comments and feedback are welcome to - dyerkb@myrealbox.com ; -- Code was inspired from - http://members.ams.chello.nl/a.westra2/800/prtshr8i.htm (no, I don't know Dutch :))
BREAK ON CLS :setprinter IF "%OS%" <> "Windows_NT" ; -- is it winnt or 2K? RETURN ; Not Windows NT/2K/XP (ADDRPRINTERCONNECTION AND SETDEFAULTPRINTER are not supported), so return ELSE $debug = "No" ; -- Set this to Yes for displaying the results $ps = "%windir%\Printsetup.ini" ; -- Print Configuration File ; -- Read default printer string from registry $subkey = "HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows" $value = ReadValue( $subkey, "Device" ) ;?$value ; -- We Need to test for the existence of a Network Share being used ; -- This is done in the event of someone using Acrobat Writer ; -- Or a FAX driver instead of a networked printer as default IF 0 = InStr($value, "\\") RETURN ; -- If they are using a local printer driver, return ; -- Otherwise, go on to delete the networked printer and re-add it ENDIF
; -- If $VALUE = "", does a printer exist in the Registry? IF $value = "" ; -- We should check for the INI File IF 1 = Exist($ps) ; -- The INI File does exist, let's read it and use the info $def=READPROFILESTRING($ps,"Printers","Default") $ptr=READPROFILESTRING($ps,"Printers","PtrName") AddPrinterConnection ($def) ; -- Add the printer (only available on NT/2K) SetDefaultPrinter ($ptr) ; -- Set the Printer to Default ;ELSE ; RETURN ; -- Printer does not exist in the Registry nor in the INI File, maybe a message here? ENDIF ENDIF
$offset = InStr( $value, "," ) ; Parse UNC only from string IF $debug = "Yes" ?$offset + " Offset" ENDIF $len = $offset - 1 IF $debug = "Yes" ?$len + " Length" ENDIF $lpt1 = SUBSTR( $value, 1, $len )
IF $debug = "Yes" ; Display UNC value found ? "LPT1 = " + $LPT1 + " (Default Printer)" ENDIF
; -- Now, we have the Default Printer, let's capture it!! ; -- While we have the chance, let's capture the printer for any new users that may use the machine ;IF 0 <> EXIST"%windir%\Printsetup.ini" ; -- Uncomment this line as well as the next endif to leave the INI file alone $RC=WRITEPROFILESTRING($ps,"Printers","Default",$lpt1) ; -- Get the whole UNC Path and Printer
$nlen = $offset - 3 IF $debug = "Yes" ?$nlen + " New Length" ENDIF
; OK, So now let's remove the \\ $prtn = SUBSTR( $value, 3, $nlen ) IF $debug = "Yes" ?$prtn + " Removed \\" ENDIF
; Now, Let's grab the printer name $offset1 = INSTR( $prtn, "\" ) $offset1 = $offset1 + 1 IF $debug = "Yes" ?$offset1 + " Offset1" ENDIF $len1 = $offset1 IF $debug = "Yes" ?$len1 + " Second New Length" ENDIF
$lptn = SUBSTR( $prtn, $offset1, $len1 ) IF $debug = "Yes" ?$lptn + " Printer Name" ENDIF $RC=WRITEPROFILESTRING($ps,"Printers","PtrName",$lptn) ; -- Capture the Name of the Printer ;EndIf ; -- Done writing to the INI file
; -- Actual mapping - Re-Map the printer for LPT1 ; -- CHECK TO SEE IF LPT1 WAS DONE WITH A NET USE IF 0 = ExistKey("HKEY_CURRENT_USER\Network\LPT1") ; -- If the printer has been mapped then delete it to refresh the connection ; -- Delete the printer USE LPT1: $lpt1 /del /persistent ; -- Refresh the connection to the printer as LPT1 with the string captured above USE LPT1: $lpt1 /persistent RETURN ELSE ; -- ASSUME THE PRINTER HAS NOT BEEN DONE WITH A NET USE COMMAND ; -- Add it in as LPT1 anyway USE LPT1: $lpt1 /persistent RETURN ENDIF RETURN ENDIF
I look at this code and it seems pretty intensive for a relatively simple task.
Questions:
- Am I doing too much?
- Should the approach be done on an array?
- Could this be done in a different way?
Thank you. Kent
|
|
Top
|
|
|
|
#71504 - 2002-11-10 07:39 AM
Re: RFC - Clean-up of FAQ Post for Printer Setup/Capture
|
Kdyer
KiX Supporter
   
Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
|
I was thinking about the @INWIN when I posted it here.. I don't think I have the whole updated script from work here.
Let me try this again as we don't use W9x anymore.
code:
;BREAK ON ;CLS $debug_mode = "No" ; -- Set this to Yes to see what is going on $windir = READVALUE("HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion","SystemRoot") $systemdrive = SUBSTR($windir,1,2) ; -- This will show C:\ or M:\ or what drive windows is installed on
IF exist("H:\nul") $ps = "H:\Scripts_DO_NOT_DELETE\Printsetup.ini" ; -- Print Configuration File IF GETFILEATTR("H:\Scripts_DO_NOT_DELETE") & 16 ELSE SHELL "%comspec% /c md H:\Scripts_DO_NOT_DELETE" ENDIF IF EXIST($windir+"\Printsetup.ini") DEL $windir+"\Printsetup.ini" ENDIF ELSE IF EXIST($windir+"\Printsetup.ini") DEL $windir+"\Printsetup.ini" ENDIF IF exist($systemdrive+"\Scripts\nul") $ps = $systemdrive+"\Scripts\Printsetup.ini" ; -- Print Configuration File ELSE SHELL "%comspec% /c md "+$systemdrive+"\Scripts" $ps = $systemdrive+"\Scripts\Printsetup.ini" ; -- Print Configuration File ENDIF ENDIF $subkey = "HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows" $value = ReadValue( $subkey, "Device" ) ; -- Read default printer string from registry IF $debug_mode = "Yes" ?$value ENDIF ; -- We Need to test for the existence of a Network Share being used ; -- This is done in the event of someone using Acrobat Writer ; -- Or a FAX driver instead of a networked printer as default
IF 0 <> InStr($value, "") ; -- IT IS NOT NULL IF $debug_mode = "Yes" ? "Something found" ENDIF IF 0 <> InStr($value, "\\") ; -- IT HAS \\ - network Printer IF $debug_mode = "Yes" ? "Network Printer" ENDIF ELSE ; -- DO NOT DO ANYTHING IF $debug_mode = "Yes" ? "FAX or Acrobat" ENDIF RETURN ENDIF ENDIF
IF 0 = InStr($value, "") ; -- IT IS NULL IF $debug_mode = "Yes" ? "NO PRINTER FOUND" ENDIF ; -- We should check for the INI File IF 1 = Exist($ps) ; -- The INI File does exist, let's read it and use the info $def=READPROFILESTRING($ps,"Printers","Default") $ptr=READPROFILESTRING($ps,"Printers","PtrName") $rc = AddPrinterConnection ($def) ; -- Add the printer (only available on NT/2K) $rc = SetDefaultPrinter ($def) ; -- Set the Printer to Default $value = ReadValue( $subkey, "Device" ) ; -- Read default printer string from registry ENDIF ENDIF
IF 0 = InStr($value, "") ; -- IT IS NULL RETURN ; -- NO PRINTER NOR INI FILE FOUND ENDIF
; -- Let's manipulate some strings
$offset = InStr( $value, "," ) ; Parse UNC only from string IF $debug_mode = "Yes" ?"Offset "+$offset ENDIF $len = $offset - 1 IF $debug_mode = "Yes" ?"Length "+$len ENDIF
$lpt1 = SUBSTR( $value, 1, $len )
; Display UNC value found IF $debug_mode = "Yes" ? "LPT1 = " + $lpt1 + " (Default Printer)" ENDIF
; -- Now, we have the Default Printer, let's capture it!! ; -- While we have the chance, let's capture the printer for any new users that may use the machine ;IF 0 <> EXIST"%windir%\Printsetup.ini" ; -- Uncomment this line as well as the next endif to leave the INI file alone
$ = WRITEPROFILESTRING($ps,"Printers","Default",$lpt1) ; -- Get the whole UNC Path and Printer
$nlen = $offset - 3 IF $debug_mode = "Yes" ?"New Length "+$nlen ENDIF
; OK, So now let's remove the \\ $prtn = SUBSTR( $value, 3, $nlen ) IF $debug_mode = "Yes" ?$prtn + " Removed \\" ENDIF
; Now, Let's grab the printer name $offset1 = INSTR( $prtn, "\" ) $offset1 = $offset1 + 1 IF $debug_mode = "Yes" ?"Offset1 " + $offset1 ENDIF
$len1 = $offset1 IF $debug_mode = "Yes" ?"New Length1 " + $len1 ENDIF
$lptn = SUBSTR( $prtn, $offset1, $len1 )
IF $debug_mode = "Yes" ?$lptn + " Printer Name" ? ?"Script is complete, Press Any Key to continue" GET $k ENDIF
$ = WRITEPROFILESTRING($ps,"Printers","PtrName",$lptn) ; -- Capture the Name of the Printer
;EndIf ; -- Done writing to the INI file
; -- Actual mapping - Re-Map the printer for LPT1 ; -- CHECK TO SEE IF LPT1 WAS DONE WITH A NET USE IF 0 = ExistKey("HKEY_CURRENT_USER\Network\LPT1") ; -- If the printer has been mapped then delete it to refresh the connection ; -- Delete the printer USE LPT1: $lpt1 /del /persistent ; -- Refresh the connection to the printer as LPT1 with the string captured above USE LPT1: $lpt1 /persistent RETURN ELSE ; -- ASSUME THE PRINTER HAS NOT BEEN DONE WITH A NET USE COMMAND ; -- Add it in as LPT1 anyway USE LPT1: $lpt1 /persistent RETURN ENDIF
Kent [ 10. November 2002, 16:20: Message edited by: kdyer ]
|
|
Top
|
|
|
|
#71505 - 2002-11-11 08:31 PM
Re: RFC - Clean-up of FAQ Post for Printer Setup/Capture
|
Anonymous
Anonymous
Unregistered
|
Kdyer- I am having a few problems with this script. 1) ; -- DO NOT DO ANYTHING IF $debug_mode = "Yes" ? "FAX or Acrobat" ENDIF RETURN
I have set the default to the local printer. When it reaches Return, here, the script quits. So I went ahead and ";" it out
Later on, when the script gets to the Use LPT1: $lpt1 /persistent The scipt gives me a use error and spits out return.
I guess my problem is that when the default is a local printer, it does not capture the printer. Any help?
|
|
Top
|
|
|
|
#71507 - 2002-11-11 11:03 PM
Re: RFC - Clean-up of FAQ Post for Printer Setup/Capture
|
Anonymous
Anonymous
Unregistered
|
Ok- One problem with the script that may or may not be a problem When using the following: Use LPT1: $lpt1 /persistent It does not work. I had to make it work by sharing the local printer. Then the following line works: USE LPT1: "\\@wksta\$lpt1" /persistent
Can you map a local printer to the lpt1 port without sharing the local printer? If so, what would need to be done?
|
|
Top
|
|
|
|
#71509 - 2002-11-12 05:31 AM
Re: RFC - Clean-up of FAQ Post for Printer Setup/Capture
|
Kdyer
KiX Supporter
   
Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
|
Jens raises a good point. If you had a printer on the USB port, for example you would do "printer pooling" use it on LPT1 too.
However... if you want.. This maybe it..
OK.. Let's whittle this down a bit.
code:
$subkey = "HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows" $value = ReadValue( $subkey, "Device" ) IF 0 = InStr($value, "") ; -- IT IS NULL RETURN ; -- NO PRINTER NOR INI FILE FOUND ENDIF
; -- Let's manipulate some strings
$offset = InStr( $value, "," ) ; Parse name from string $len = $offset - 1 $lpt1 = SUBSTR( $value, 1, $len )
IF (ExistKey("HKEY_CURRENT_USER\Network\LPT1") = 0) ; -- If the printer has been mapped then delete it to refresh the connection ; -- Delete the printer USE LPT1: $lpt1 /del /persistent ; -- Refresh the connection to the printer as LPT1 with the string captured above USE LPT1: $lpt1 /persistent RETURN ELSE ; -- ASSUME THE PRINTER HAS NOT BEEN DONE WITH A NET USE COMMAND ; -- Add it in as LPT1 anyway USE LPT1: $lpt1 /persistent RETURN ENDIF
HTH,
Kent
|
|
Top
|
|
|
|
#71510 - 2002-11-12 05:47 PM
Re: RFC - Clean-up of FAQ Post for Printer Setup/Capture
|
Anonymous
Anonymous
Unregistered
|
Mapping is a poor choice of words. What I want to do is capture the local printer to the ini file, then make that printer the default printer....each time they logon. Is the best way to do this through the registry? Do I use the setdefaultprinter command on a local printer?
|
|
Top
|
|
|
|
#71511 - 2002-11-12 05:57 PM
Re: RFC - Clean-up of FAQ Post for Printer Setup/Capture
|
Kdyer
KiX Supporter
   
Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
|
This should do it..
code:
IF exist("H:\nul") $ps = "H:\Scripts_DO_NOT_DELETE\Printsetup.ini" ; -- Print Configuration File IF GETFILEATTR("H:\Scripts_DO_NOT_DELETE") & 16 ELSE SHELL "%comspec% /c md H:\Scripts_DO_NOT_DELETE" ENDIF IF EXIST($windir+"\Printsetup.ini") DEL $windir+"\Printsetup.ini" ENDIF ELSE IF EXIST($windir+"\Printsetup.ini") DEL $windir+"\Printsetup.ini" ENDIF IF exist($systemdrive+"\Scripts\nul") $ps = $systemdrive+"\Scripts\Printsetup.ini" ; -- Print Configuration File ELSE SHELL "%comspec% /c md "+$systemdrive+"\Scripts" $ps = $systemdrive+"\Scripts\Printsetup.ini" ; -- Print Configuration File ENDIF ENDIF
$subkey = "HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows" $value = ReadValue( $subkey, "Device" ) IF 0 = InStr($value, "") ; -- IT IS NULL RETURN ; -- NO PRINTER NOR INI FILE FOUND ENDIF
; -- Let's manipulate some strings
$offset = InStr( $value, "," ) ; Parse name from string $len = $offset - 1 $lpt1 = SUBSTR( $value, 1, $len )
$RC = WRITEPROFILESTRING($ps,"Printers","Default",$lpt1)
HTH,
Kent
|
|
Top
|
|
|
|
#71512 - 2002-11-12 09:31 PM
Re: RFC - Clean-up of FAQ Post for Printer Setup/Capture
|
Anonymous
Anonymous
Unregistered
|
Thanks for everyone's help. I got it to work. Conclusion: The script captures to an .ini file and then reads the file the next time to reset as default. Thanks for the help
|
|
Top
|
|
|
|
#71513 - 2002-11-12 10:02 PM
Re: RFC - Clean-up of FAQ Post for Printer Setup/Capture
|
Kdyer
KiX Supporter
   
Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
|
Can you post/share your final code?
This I am sure will come up again and we can post this as an addendum to the FAQ too.
Kent
|
|
Top
|
|
|
|
#71514 - 2002-11-13 12:41 AM
Re: RFC - Clean-up of FAQ Post for Printer Setup/Capture
|
Anonymous
Anonymous
Unregistered
|
I'd be honored. I changed a few things, and the intent of the script. The intent is to take a snapshot of the default printer. From them on, each time the user logs on, the user reads the ini file and sets that as the default printer. Now the only thing I need to do is to make it only for people with local printers. If its a usb printer port, it would be hklm\system\controlser001\services\usbprint Obviously, if LPT1 is hkeyCU\network\lpt1 I'll keep on looking. Here is the meat and the potatoes of it:
code:
;---Declares Values $debug_mode = "No" ; -- Set this to Yes to see what is going on $windir = READVALUE("HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion","SystemRoot") $systemdrive = SUBSTR($windir,1,2) ; -- This will show C:\ or M:\ or what drive windows is installed on $subkey = "HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows" $value = ReadValue( $subkey, "Device" ) ; -- Read default printer string from registry
;Determines where windows is installed and creates .ini file IF exist("c:\nul") $ps = "c:\Scripts_DO_NOT_DELETE\Printsetup.ini" ; -- Print Configuration File IF GETFILEATTR("c:\Scripts_DO_NOT_DELETE") & 16 ELSE SHELL "%comspec% /c md C:\Scripts_DO_NOT_DELETE" ENDIF ;IF EXIST($windir+"\Printsetup.ini") ; DEL $windir+"\Printsetup.ini" ;ENDIF ELSE ; If there is no c drive, put it on the systemdrive IF EXIST($windir+"\Printsetup.ini") DEL $windir+"\Printsetup.ini" ENDIF IF EXIST($systemdrive+"\Scripts\nul") $ps = $systemdrive+"\Scripts\Printsetup.ini" ; -- Print Configuration File ELSE SHELL "%comspec% /c md "+$systemdrive+"\Scripts" $ps = $systemdrive+"\Scripts\Printsetup.ini" ; -- Print Configuration File ENDIF ENDIF
IF $debug_mode = "Yes" ?$value ENDIF
;Reads string from .ini IF EXIST("$ps") IF $debug_mode = "Yes" ? "NO PRINTER FOUND" ENDIF ; -- We should check for the INI File $def=READPROFILESTRING($ps,"Printers","Default") $ptr=READPROFILESTRING($ps,"Printers","PtrName") SetDefaultPrinter ($def) ; -- Set the Printer to Default Goto End ENDIF
;Let's manipulate some strings For Default Printer $offset = InStr( $value, "," ) ; Parse UNC only from string IF $debug_mode = "Yes" ?"Offset "+$offset ENDIF $len = $offset - 1 IF $debug_mode = "Yes" ?"Length "+$len ENDIF
$lpt1 = SUBSTR( $value, 1, $len )
; Display UNC value found IF $debug_mode = "Yes" ? "LPT1 = " + $lpt1 + " (Default Printer)" ENDIF
;-Now, we have the Default Printer, let's capture it!!-------- $ = WRITEPROFILESTRING($ps,"Printers","Default",$lpt1) ; -- Get the whole UNC Path and Printer $nlen = $offset - 3 IF $debug_mode = "Yes" ?"New Length "+$nlen ENDIF ; OK, So now let's remove the \\ $prtn = SUBSTR( $value, 3, $nlen ) IF $debug_mode = "Yes" ?$prtn + " Removed \\" ENDIF ; Now, Let's grab the printer name $offset1 = INSTR( $prtn, "\" ) $offset1 = $offset1 + 1 IF $debug_mode = "Yes" ?"Offset1 " + $offset1 ENDIF $len1 = $offset1 IF $debug_mode = "Yes" ?"New Length1 " + $len1 ENDIF $lptn = SUBSTR( $prtn, $offset1, $len1 )
IF $debug_mode = "Yes" ?$lptn + " Printer Name" ? ;?"Script is complete, Press Any Key to continue" ;GET $k ENDIF $ = WRITEPROFILESTRING($ps,"Printers","PtrName",$lptn) ; -- Capture the Name of the Printer :End
[ 13. November 2002, 18:25: Message edited by: jojopuppyfish ]
|
|
Top
|
|
|
|
#71518 - 2002-11-13 06:26 PM
Re: RFC - Clean-up of FAQ Post for Printer Setup/Capture
|
Anonymous
Anonymous
Unregistered
|
Sorry everyone. I went ahead and reedited my above post with the correct code and /code Please let me know if there any more problems.
|
|
Top
|
|
|
|
#71519 - 2002-11-21 11:02 PM
Re: RFC - Clean-up of FAQ Post for Printer Setup/Capture
|
Kdyer
KiX Supporter
   
Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
|
Hi everybody,
I have cleaned up Default Printer Setup Script and have added in jojopuppyfish's code as well. This is also cleaned up.
This is reflected in the FAQ.
Thanks,
Kent
|
|
Top
|
|
|
|
#71520 - 2002-11-23 07:45 AM
Re: RFC - Clean-up of FAQ Post for Printer Setup/Capture
|
Kdyer
KiX Supporter
   
Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
|
I believe that I have addressed Jens point about re-adding the printer on each login.
This has been updated in the FAQ section too.. Default Printer Configuration
Here is what the cleaned up code distills down to.
code:
BREAK ON CLS $subkey = "HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows" $windir = READVALUE("HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion","SystemRoot") $systemdrive = SUBSTR($windir,1,2) IF EXIST($windir+"\Printsetup.ini") DEL $windir+"\Printsetup.ini" ENDIF IF (GETFILEATTR("H:") & 16) $ps = "H:\Scripts_DO_NOT_DELETE\Printsetup.ini" IF ((GETFILEATTR("H:\Scripts_DO_NOT_DELETE") & 16) = 0) MD "H:\Scripts_DO_NOT_DELETE" ENDIF ELSE $ps = $systemdrive+"\Scripts\Printsetup.ini" IF ((GETFILEATTR($systemdrive+"\Scripts") & 16) = 0) MD $systemdrive+"\Scripts" ENDIF ENDIF $value = ReadValue($subkey,"Device") IF Exist($ps) $lpt1 = READPROFILESTRING($ps,"Printers","Default") $rc = AddPrinterConnection($lpt1) $rc = SetDefaultPrinter($lpt1) ELSE $lpt1 = SUBSTR($value,1,InStr($value,",")-1) $rc = AddPrinterConnection($lpt1) $rc = SetDefaultPrinter($lpt1) ENDIF SLEEP 1 IF INSTR($value,"\\") $rc = WRITEPROFILESTRING($ps,"Printers","Default",$lpt1) USE LPT1: $lpt1 /del /persistent USE LPT1: $lpt1 /persistent ENDIF RETURN
Comments and suggestions are welcome.
Thanks,
Kent
|
|
Top
|
|
|
|
Moderator: Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart
|
0 registered
and 1046 anonymous users online.
|
|
|