Page 1 of 1 1
Topic Options
#204071 - 2012-01-15 04:45 PM printer script HELP
Chan Offline
Fresh Scripter

Registered: 2011-10-14
Posts: 15
Loc: Arizona
Hi all,
it's been a wile since my last post but was wondering is someoone see what i am missing... i have a simple printer script that run throught with no issue the on issue is the printer never gets added even though it say it does,, (Windoes 7)


 Code:
; * * *   P R I N T E R  S C R I P T * * *

; Establish script global variables
	$i1 = 0    
  	$i2 = 0
  	$sRegValEnum = ""
  	$sRegKeyEnum = ""
  	$sRunThis = ""
	$sRegkey3 = "HKEY_CURRENT_USER\Printers\Connections\,,xxxxxxxxxxxx,"
	$i1 = 0
	$sPrinterExist = ""
	$sRegKey4 = "HKEY_CURRENT_USER\Printers\Connections"
	$sSearchFor2 = "xxxxxxxxxxxx"
	$sSearchFor3 = "LPT"
	$SOffset = ""
	$sOffset2 = ""
	$sDefaultPrinter = ""
	$sDefaultALocalPrinter = ""
	$sNumberOfPrinters = 0
  	$sPrinterPrefix = ""
	$ipbyte3 = Val (LTrim (SubStr( @IPADDRESS0 , 9 ,3)))
	$LoopCount = 0
    
;********************************************************************************
;*	Build the array $aPrinters based on the value in string $ipbyte3
;*	Set the number of printers in $sNumberOfPrinters and 
;*	set the value for $sPrinterPrefix used later in the script.
;*	Jump to the subAddPrinters subroutine
;********************************************************************************

SELECT
	CASE $IPBYTE3 = xxx		; Values for Tempe floor printers
		$sNumberOfPrinters = 1
		DIM  $aPrinters[1]
		$aPrinters[0] = "HP-LaserJet 4 Plus)"
		? "Printer array created."
		GOSUB SubAddPrinters
		GOTO PrinterCheckLoop
		
	

	CASE 1	;If CASE is not meet execute the following commands 
		? "unknown case type (subnet)"
		GOTO End_Printers

ENDSELECT

;********************************************************************************
;* Enterprise Standard Process - Except edit 	IF NOT INGROUP("xx xxxx-xxxxx")
;********************************************************************************


:SubAddPrinters
;********************************************************************************
;*	Check the registry location ($sRegKey3[$i4] + 
;*	$aPrinters) to determine if printer $aPrinters[$i4]
;*	is currently installed. Otherwise use ADDPRINTERCONNECTION 
;*	to add this printer to the local system.
;********************************************************************************
 
? "  Number of network printers on this floor = " + $sNumberOfPrinters
DO
	;? "Index is " + $i1
	? "    Check for printer  " + $aPrinters[$i1]
	$sPrinterExists = EXISTKEY($sRegKey3 + $aPrinters[$i1])
	IF @ERROR = 0 
		? "Printer " + $aPrinters[$i1] + " already exists"
	ELSE
		$temp=Addprinterconnection("\\"+$sSearchFor2+"\" + $aPrinters[$i1])
		? "      "+$aPrinters[$i1] " created."
	ENDIF
	$i1 = $i1 + 1
UNTIL  $i1 = $sNumberOfPrinters
? ""

$sPrinterExist = ""
$i1 = 0
RETURN
CLS


:PrinterCheckLoop
;********************************************************************************
;*	Module To Remove Printer Mappings To Other Sites For Travelers		*
;********************************************************************************

	IF NOT INGROUP("xxx xxxx-xxxxx")
		GOTO CheckDefaultPrinter
	ELSE
	        $LoopCount = $LoopCount + 1
	        IF $LoopCount > 50
	                   MESSAGEBOX("There is an issue with one or more of the installed Printer Queues. Please submit a Work Request with Desktop Services to have this condition corrected","Issue Removing Print Queue",16,30) 
	                   $sRegKeyEnum = ""
	                   $i1 = 0		;// Environment cleanup
	                   $sOffset = ""
	                   $LoopCount = 0
	                   GOTO CheckDefaultPrinter
	        ENDIF     
	ENDIF
? "      Checking for Print Queues created by other sites" 
$sRegKeyEnum = EnumKey($sRegKey4, $i1)
IF @ERROR = 0
	DO
      	$sOffset = INSTR($sRegKeyEnum, $sSearchFor2)

	If $sOffset = 0
		? "Remove printer "+ $sRegKeyEnum
       		$PrinterString = SUBSTR($sRegKeyEnum, 3 ,LEN($sRegKeyEnum))
       	  	$TheComma = INSTR($PrinterString, ",")
       	  	$ServerName = SUBSTR($PrinterString, 1, ($TheComma-1))
       	  	$PrinterName = SUBSTR($PrinterString, ($TheComma+1),LEN($PrinterString))
       	  	DelPrinterConnection("\\" + $ServerName + "\" + $PrinterName)
       	  	GOTO PrinterCheckLoop
      	ELSE
        	? "Printer "+ $sRegKeyEnum + " is valid for this location"    
   	ENDIF    

      	$i1 = $i1 + 1
      	$sRegKeyEnum = EnumKey($sRegKey4, $i1)
        IF $i1 = 50
                   $sRegKeyEnum = ""
                   MESSAGEBOX("There is an issue with one or more of the installed Printer Queues. Please contact Desktop Services via a standard Work Request to have someone correct this condition.",16,30) 
        ENDIF     
    UNTIL @ERROR <> 0 OR $sRegKeyEnum = ""
ENDIF
? ""
$i1 = 0		;// Environment cleanup
$sOffset = ""


:CheckDefaultPrinter
;********************************************************************************
;*	Check the value $sDefaultPrinter for $sServer and $sPrinterPrefix.
;*	Warn user if this value is not valid and open Printers folder
;********************************************************************************

$sDefaultPrinter = ReadValue("HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows","Device")
IF @ERROR <> 0
	MESSAGEBOX("Your default network printer is not set.  Please choose a default network printer by right-clicking its icon from the printers folder and selecting 'Set As Default'" + CHR(13) + CHR(13) + "The Printers folder will now be opened","Default Network Printer Not Set",16,30) 
    RUN "CONTROL.EXE PRINTERS"
ELSE
    $sOffset = INSTR($sDefaultPrinter, $sSearchFor2)
    $sOffset2 = INSTR($sDefaultPrinter, $sPrinterPrefix)
    IF $sOffset = 0 OR $sOffset2 = 0
      	$sSubString1 = SUBSTR($sDefaultPrinter,1,2)
      	IF $sSubString1 = "\\"
        	MESSAGEBOX("Your default network printer is not set for this site.  Please choose a default network printer by right-clicking its icon from the Printers folder and selecting 'Set As Default'" + CHR(13) + CHR(13) + "The Printers folder will now be opened","Default Network Printer Not Set",16,30) 
        	RUN '"%systemroot%\system32\CONTROL.EXE" PRINTERS'
        ELSE
      			? "Local Printer " + $sDefaultPrinter + " is the current default printer"        
      	ENDIF
    ELSE
    	? "Network Printer " + $sDefaultPrinter + " is the current default printer"        
    ENDIF
ENDIF
$sOffset = ""	; Environment cleanup
$sOffset2 = ""
$sDefaultALocalPrinter = ""


:End_Printers
? "* * * P R I N T E R  S C R I P T  C O M P L E T E D  * * *"
sleep 2


Edited by Allen (2012-01-15 05:04 PM)
Edit Reason: added code tags

Top
#204072 - 2012-01-15 05:40 PM Re: printer script HELP [Re: Chan]
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
UAC :P
_________________________
!

download KiXnet

Top
#204074 - 2012-01-16 04:25 AM Re: printer script HELP [Re: Lonkero]
Chan Offline
Fresh Scripter

Registered: 2011-10-14
Posts: 15
Loc: Arizona
Good tha is what I thought it was at first, but i have abled and configured point and print restrictions in the GPO, and have als tested it with UAC disabled... it trulesy is just not makesing any since....
Top
#204076 - 2012-01-16 02:25 PM Re: printer script HELP [Re: Chan]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
My first suggestion would be to get rid of all those GoTo's and GoSub's. They make the code messy and difficult to follow. This can easily be done with some Select-Case-Endselect or If-Else-Endif statements. See below for an example. The example below requires the PriMapState UDF from the UDF section of this board.

On this line $ipbyte3 = Val (LTrim (SubStr( @IPADDRESS0 , 9 ,3))) you are filling a variable based on the value of a left trim of a substring taken from @IPADDRESS0 \:o . You start reading at the 9th character and read 3 characters. This is not the third octet of the IP address but it results in something like “11.” or “ 1.” . The third octet starts at the 8th character and stops at the 10th. It looks like there is a lot of extra unneeded code in your script like the GoTo, GoSub, Val(Ltrim(…., etc…

If you can specify a bit what you need then we might be able to come up with a cleaner solution.
 Code:
;Set printer name.
$printer = "\\server\PrinterA"
;Check group membership.
If InGroup("GroupA")
	;Check if the printer is already installed.
	If Not PriMapState($printer)
		;Printer is not installed. Add printer.		
		$rc = AddPrinterConnection($printer)
	EndIf
EndIf

;Set printer name.
$printer = "\\server\PrinterB"
;Check group membership.
If InGroup("GroupB")
	;Check if the printer is already installed.
	If Not PriMapState($printer)
		;Printer is not installed. Add printer.		
		$rc = AddPrinterConnection($printer)
	EndIf
EndIf

;Check group membership.
If SubStr(@IPADDRESS0, 8, 3) = "111"
	$printer = "\\Server\PrinterA"
	If PriMapState($printer) <> "2"
		;Set default printer.
		$rc = SetDefaultPrinter($printer)
	EndIf
EndIf

;Check group membership.
If SubStr(@IPADDRESS0, 8, 3) = "222"
	$printer = "\\Server\PrinterB"
	If PriMapState($printer) <> "2"
		;Set default printer.
		$rc = SetDefaultPrinter($printer)
	EndIf
EndIf
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#204077 - 2012-01-16 02:50 PM Re: printer script HELP [Re: Mart]
Allen Administrator Online   shocked
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4562
Loc: USA
Is this a GPO Startup script or a logon script?
Top
#204102 - 2012-01-22 05:32 AM Re: printer script HELP [Re: Allen]
Chan Offline
Fresh Scripter

Registered: 2011-10-14
Posts: 15
Loc: Arizona
Yes GPO
Top
#204103 - 2012-01-22 06:11 AM Re: printer script HELP [Re: Chan]
Allen Administrator Online   shocked
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4562
Loc: USA
The drives and printers are being added in the context of the GPO instead of the user. This is a known problem with Vista and above.

Here are a few options...
http://www.kixtart.org/forums/ubbthreads.php?ubb=showflat&Number=198514#Post198514

Keep in mind the registry hack is unsupported by MS.

Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
1 registered (Allen) and 1198 anonymous users online.
Newest Members
M_Moore, BeeEm, min_seow, Audio, Hoschi
17883 Registered Users

Generated in 0.113 seconds in which 0.077 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org