Page 1 of 2 12>
Topic Options
#204631 - 2012-04-04 08:36 AM AddPrinterConnection -> does not work sometimes
MartinAD Offline
Fresh Scripter

Registered: 2011-08-01
Posts: 13
Loc: -
Hello KIX-Community,

I have a problem with my KIX script and I hope that some of you can help me solving this problem.

I use the Method "AddPrinterConnection" to connet a shared Printer from a central Printserver during user logon.

What happens is, the printer gets connected 99% of the time, but in 1%, the printer simply does not get connected.

I want to fix this and my first thought was, to simply catch the @ERROR value after the printer connection and do a second attempt.

while @ERROR <> 0
AddPrinterConnection()
loop

This is causing problems when a printer simply cannot be connected at all (when server is down, printer configuration wrong etc.)

How would you tackle such a problem?

thank you very much in advance for your thoughts and best regards
Martin

Top
#204632 - 2012-04-04 10:24 AM Re: AddPrinterConnection -> does not work sometimes [Re: MartinAD]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
You could use the Ping() UDF to see of the print server is up before trying to add printers.
Just pinging is not 100% bulletproof because a server might still reply to a ping when it is unresponsive to other requests but it does give you some info on the availability of the print server.

How to use a UDF
UDF library

Example:
 Code:
Break on

If Ping("printerserver", 0) <> 0
	$rc = AddPrinterConnection("\\printserver\printer")
EndIf



;FUNCTION         Ping()
;
;AUTHOR           Jochen Polster (jochenDOTpolsterATgmxDOTnet)
;
;VERSION HISTORY  1.0 - 11/24/2001 Initial Release
;                 1.1 - 11/11/2004 Fixed problems regarding spaces in folder names
;                                  of script directory (thanks to Alistair)
;                 1.2 - 12/07/2004 Fixed for NoVarsInStrings and Explicit on
;
;ACTION           Pings the Computer specified, or returns its ip address (Wins Resolved)
;
;SYNTAX           Ping(Computer,GetIP,[LoopCount],[Timeout])
;
;PARAMETERS       Computer (Required)
;                  -  String value representing the Computer to ping
;
;                 GetIp (Required)
;                  -  If specified (1), the function will return the ip-address
;                     of specified Computer rather than pinging it ...
;                     Has to be 0 if you want to check for reply !
;
;                 LoopCount (Optional but useful !)
;                  -  Integer value representing the number of times
;                     the Computer shall be pinged
;
;                 Timeout (Optional)
;                  -  if ommited the Computer gets pinged with the default
;                     timeout (1s) and default retries (4)
;
;REMARKS          If there is a reply the function will return immediately with 1
;                 so it could be faster not specifiying a timeout.
;
;RETURNS          0 - No reply
;                 1 - Ping reply
;                 Optional - ip address of specified Computer
;
;DEPENDENCIES     None
;
;EXAMPLES         call "path\Ping.udf"
;                 if Ping("www.microsoft.com",0,12,5000) ;pings for max. 60 seconds
;                 	run "%ComSpec% /c start www.microsoft.com"
;                 else
;                 	'Site isn't available ' ?
;                 endif
;                 if Ping("www.microsoft.com",0,15) ;pings also for max. 60 seconds
;                 	run "%ComSpec% /c start www.microsoft.com"
;                 else
;                 	'Site isn't available ' ?
;                 endif
;                 $ip = Ping("www.microsoft.com",1)
;                 $ip ?

Function Ping($Computer, $GetIP, optional $LoopCount, optional $TimeOut)
	If $GetIP
		Dim $ip, $ipfile, $
		$ipfile = @scriptdir + '\ip.txt'
		Shell '%Comspec% /q /e:1024 /c for /F "tokens=2 delims=[]" %%i IN (' + Chr(39)
		+ '"ping ' + $Computer + ' -n 1 | find "]""' + Chr(39) + ') do echo %%i >"' + $ipfile + '"'
		$ = Open(10, $ipfile, 2) $ip = ReadLine(10) $ = Close(10) Del $ipfile
		If $ip
			$Ping = $ip
		Else
			$Ping = 'Bad IP address ' + $Computer + '!'
		EndIf
		Exit 0
	Else
		If $TimeOut
			For $c = 0 to $LoopCount
				Shell '%Comspec% /C ping ' + $Computer + ' -n 1 -w ' + $TimeOut + ' | find /C "TTL=" > nul'
				If @error = 0
					$Ping = 1
					Exit 0
				EndIf
			Next
		Else
			For $c = 0 to $LoopCount
				Shell '%Comspec% /C ping ' + $Computer + ' | find /C "TTL=" > nul'
				If @error = 0
					$Ping = 1
					Exit 0
				EndIf
			Next
		EndIf
		$Ping = 0
	EndIf
EndFunction
]
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#204633 - 2012-04-04 12:22 PM Re: AddPrinterConnection -> does not work sometimes [Re: Mart]
MartinAD Offline
Fresh Scripter

Registered: 2011-08-01
Posts: 13
Loc: -
Thanks for the reply.

problem is that even if the server pings, I cannot be sure that the printer will get connected successfully. The printer could simply not be available on the printserver (got deleted) or is offline etc.

The other problem is, that the printserver always was online when the connection failed. I do not know WHY it did not get connected. That means, I'd ping it, connect it, but do not get the actual connection to the printer.

regards
Martin

Top
#204634 - 2012-04-04 02:19 PM Re: AddPrinterConnection -> does not work sometimes [Re: MartinAD]
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4562
Loc: USA
Although I would think you would be noticing other things, this FAQ is worth checking into... (and applies to Vista/Win 7)

Windows XP Professional Fast Logon Optimization
http://www.kixtart.org/forums/ubbthreads.php?ubb=showflat&Number=112428#Post112428

Is this happening on all PCs or just one?

Top
#204635 - 2012-04-04 02:25 PM Re: AddPrinterConnection -> does not work sometimes [Re: Allen]
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
a simple what to do is to start logging the errors:
 Code:
if @error
 shell "%comspec% /c echo addprinter failed on printer_lpr_112 with error:"+@error+" "+@serror+" >>%temp%\logon.log"
endif


if you want your users to react to it and give you a call instead, just use messagebox.
_________________________
!

download KiXnet

Top
#204636 - 2012-04-04 02:27 PM Re: AddPrinterConnection -> does not work sometimes [Re: Allen]
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4562
Loc: USA
 Code:
if addprinterconnection("\\server\share")<>0
  ;create a message on the screen with messagebox and @serror
  ;or create a log in shared directory with @serror
  ;or retry the connection attempt
endif

Top
#204639 - 2012-04-05 07:22 AM Re: AddPrinterConnection -> does not work sometimes [Re: Allen]
MartinAD Offline
Fresh Scripter

Registered: 2011-08-01
Posts: 13
Loc: -
 Originally Posted By: Allen
Although I would think you would be noticing other things, this FAQ is worth checking into... (and applies to Vista/Win 7)

Windows XP Professional Fast Logon Optimization
http://www.kixtart.org/forums/ubbthreads.php?ubb=showflat&Number=112428#Post112428

Is this happening on all PCs or just one?

We have already set up that GPO.

 Quote:
a simple what to do is to start logging the errors:

this is what I am going to do next, thanks!

The thing I do not understand is that, altough the printserver is fine and the network works like a charm, printers simly get not connected sometimes. I use the same script for 800 users and 2 or 3 of them told me about the problem. That is strange. But I believe there could be a million reasons why the connection fails and a simple retry/error logging should probably get rid of most of these errors.

thanks for sharing your thoughts!


Edited by MartinAD (2012-04-05 07:22 AM)

Top
#204641 - 2012-04-05 01:31 PM Re: AddPrinterConnection -> does not work sometimes [Re: MartinAD]
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
just out of curiocity, what are your printserver OS'es?
_________________________
!

download KiXnet

Top
#204747 - 2012-04-17 12:48 PM Re: AddPrinterConnection -> does not work sometimes [Re: Lonkero]
MartinAD Offline
Fresh Scripter

Registered: 2011-08-01
Posts: 13
Loc: -
Its Win Server 2003
Top
#204748 - 2012-04-17 02:59 PM Re: AddPrinterConnection -> does not work sometimes [Re: MartinAD]
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
well... in that case
 Code:
if @error
? " error occured during printer add: " @error " (" @serror ")"
? "please, report the error number to the administration" ?
get $
endif
_________________________
!

download KiXnet

Top
#205205 - 2012-05-08 09:21 AM Re: AddPrinterConnection -> does not work sometimes [Re: Lonkero]
MartinAD Offline
Fresh Scripter

Registered: 2011-08-01
Posts: 13
Loc: -
Hi again,

i addedd the following function to my code:

 Code:
function tryCommand($command, $howoften, OPTIONAL $interval)
DIM $i
DIM $ERROR
$i = 0

if $howoften < 1 
	$tryCommand = -1
	return $tryCommand
endif

do
	$ERROR = execute($command)
	if $ERROR = 0 
		$tryCommand = $ERROR
		return $tryCommand
	endif
	
	if $interval <> ""
		sleep $interval
	endif
	
	$i = $i + 1
until $i = $howoften

$tryCommand = $ERROR
return $tryCommand

endfunction



The function "tries" to execute a passed command and is supposed to return the exit code.
You also have to tell the function how often it should try to execute the command and you can also define a interval between each execution.

for some reason, i only get 0 return codes. Even if I try to "use" a share that is already in use or a printer is not able to connect, I always get 0 as a return code.

I dont know why, maybe this behaviour is by design or I have done something wrong. Maybe you can share some ideas.

thanks for reading
Martin.


Edited by MartinAD (2012-05-08 09:22 AM)

Top
#205206 - 2012-05-08 09:40 AM Re: AddPrinterConnection -> does not work sometimes [Re: MartinAD]
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
try:
 Code:
$ = execute($command)
$ERROR = @error
_________________________
!

download KiXnet

Top
#205269 - 2012-05-18 01:45 PM Re: AddPrinterConnection -> does not work sometimes [Re: Lonkero]
MartinAD Offline
Fresh Scripter

Registered: 2011-08-01
Posts: 13
Loc: -
still the same.

I call the function multiple time with the same command, like this:
 Code:
tryCommand("use e: \\fileserver\share1", 3)
tryCommand("use e: \\fileserver\share1", 3)
tryCommand("use e: \\fileserver\share1", 3)



Although the share gets connected successful, I always get a 0 return code. I don't get it, is the use command not supposed to throw an error if the letter is already in use?

this is the function now, ?-Output created for debug purposes:

 Code:
function tryCommand($command, $howoften, OPTIONAL $interval)

DIM $i
DIM $ERROR
$i = 0

if $howoften < 1 
	$tryCommand = -1
	return $tryCommand
endif

do
	$ = execute($command)
	$ERROR = @error
	?"Error is: "+$ERROR
	?"@ERROR is: "+@ERROR
	DIM $foo
	get $foo
	?
	
	
	if $ERROR = 0 
		$tryCommand = $ERROR
		return $tryCommand
	endif
	
	if $interval <> ""
		sleep $interval
	endif
	
	$i = $i + 1
until $i = $howoften

$tryCommand = $ERROR
return $tryCommand

endfunction








Edited by MartinAD (2012-05-18 01:51 PM)

Top
#205270 - 2012-05-18 01:50 PM Re: AddPrinterConnection -> does not work sometimes [Re: MartinAD]
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
I guess execute will always work and reset the error to 0.

your use command UNC is not encapsulated...

change your execute to:
 Code:
$ = execute($command + " " + $ERROR = @error)


to be whitespace safe, change your use line to:
 Code:
tryCommand("use 'e:' '\\fileserver\share1'")
_________________________
!

download KiXnet

Top
#205271 - 2012-05-18 01:58 PM Re: AddPrinterConnection -> does not work sometimes [Re: Lonkero]
MartinAD Offline
Fresh Scripter

Registered: 2011-08-01
Posts: 13
Loc: -
 Quote:
I guess execute will always work and reset the error to 0.


this was exactly my thought and so I tried to verify this. This is what the actual manual says:
 Quote:
Execute

Returns:
The exitcode of the executed script.


As far as I understand it, its not the exit code of the execute command, its the exit code of the executed script.

I adjusted my script as you suggested, still not luck. Share gets connected, return code always 0.


Edited by MartinAD (2012-05-18 02:04 PM)

Top
#205272 - 2012-05-18 03:13 PM Re: AddPrinterConnection -> does not work sometimes [Re: MartinAD]
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
simple test:
 Code:
use c: \\127.0.0.1\share
@error " " @serror ?
$=execute("use c: '\\127.0.0.1\share' $$ERROR=@@error")
@error " " @serror ?
"the actual error: " $ERROR ?


reveals that execute indeed resets the exit code.

my previous example execute was wrong, cause kixtart actually did $error = @error as comparison and added that to the code to execute, so you didn't get a return val.
_________________________
!

download KiXnet

Top
#205275 - 2012-05-18 06:36 PM Re: AddPrinterConnection -> does not work sometimes [Re: Lonkero]
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
Sounds like a job for. . . the Executioner! Who was it we hung that title on? Was it Cappy?
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#205276 - 2012-05-18 07:04 PM Re: AddPrinterConnection -> does not work sometimes [Re: Les]
ShaneEP Moderator Offline
MM club member
*****

Registered: 2002-11-29
Posts: 2127
Loc: Tulsa, OK
Once I set the novars and nomacros options it seems to work for me like this...

 Code:
$nul = SetOption("NoVarsInStrings","On")
$nul = SetOption("NoMacrosInStrings","On")

use c: \\127.0.0.1\share
@error " " @serror ?
$=execute("use c: '\\127.0.0.1\share' $ERROR=@error")
@error " " @serror ?
"the actual error: " $ERROR ?

get $

Top
#205277 - 2012-05-18 07:04 PM Re: AddPrinterConnection -> does not work sometimes [Re: ShaneEP]
ShaneEP Moderator Offline
MM club member
*****

Registered: 2002-11-29
Posts: 2127
Loc: Tulsa, OK
The @error still return 0, but the $ERROR at least now contains the error.
Top
#205278 - 2012-05-18 07:13 PM Re: AddPrinterConnection -> does not work sometimes [Re: ShaneEP]
ShaneEP Moderator Offline
MM club member
*****

Registered: 2002-11-29
Posts: 2127
Loc: Tulsa, OK
Give this a shot...

 Code:
$nul = SetOption("NoVarsInStrings","On")
$nul = SetOption("NomacrosInStrings ","On")

tryCommand("use c: '\\fileserver\share1'", 3)
tryCommand("use c: '\\fileserver\share1'", 3)
tryCommand("use c: '\\fileserver\share1'", 3)
get $

function tryCommand($command, $howoften, OPTIONAL $interval)
   DIM $i
   DIM $ERROR
   $i = 0

   if $howoften < 1 
      $tryCommand = -1
      return $tryCommand
   endif

   do
      $ = execute($command+" $ERROR = @error")
      ;$ERROR = @error
      ?"Error is: "+$ERROR
      ?"@ERROR is: "+@ERROR
      DIM $foo
      get $foo
      ?

      if $ERROR = 0 
         $tryCommand = $ERROR
         return $tryCommand
      endif

      if $interval <> ""
         sleep $interval
      endif

      $i = $i + 1
   until $i = $howoften

   $tryCommand = $ERROR
   return $tryCommand
endfunction

Top
Page 1 of 2 12>


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

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

Generated in 0.079 seconds in which 0.029 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