#204631 - 2012-04-04 08:36 AM
AddPrinterConnection -> does not work sometimes
|
MartinAD
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
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:
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
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
|
|
|
|
#204636 - 2012-04-04 02:27 PM
Re: AddPrinterConnection -> does not work sometimes
[Re: Allen]
|
Allen
KiX Supporter
   
Registered: 2003-04-19
Posts: 4562
Loc: USA
|
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
Fresh Scripter
Registered: 2011-08-01
Posts: 13
Loc: -
|
We have already set up that GPO.
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
|
|
|
|
#204747 - 2012-04-17 12:48 PM
Re: AddPrinterConnection -> does not work sometimes
[Re: Lonkero]
|
MartinAD
Fresh Scripter
Registered: 2011-08-01
Posts: 13
Loc: -
|
Its Win Server 2003
|
|
Top
|
|
|
|
#205205 - 2012-05-08 09:21 AM
Re: AddPrinterConnection -> does not work sometimes
[Re: Lonkero]
|
MartinAD
Fresh Scripter
Registered: 2011-08-01
Posts: 13
Loc: -
|
Hi again,
i addedd the following function to my 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
|
|
|
|
#205269 - 2012-05-18 01:45 PM
Re: AddPrinterConnection -> does not work sometimes
[Re: Lonkero]
|
MartinAD
Fresh Scripter
Registered: 2011-08-01
Posts: 13
Loc: -
|
still the same.
I call the function multiple time with the same command, like this:
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:
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
|
|
|
|
#205271 - 2012-05-18 01:58 PM
Re: AddPrinterConnection -> does not work sometimes
[Re: Lonkero]
|
MartinAD
Fresh Scripter
Registered: 2011-08-01
Posts: 13
Loc: -
|
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: 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
|
|
|
|
#205276 - 2012-05-18 07:04 PM
Re: AddPrinterConnection -> does not work sometimes
[Re: Les]
|
ShaneEP
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...
$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
|
|
|
|
#205278 - 2012-05-18 07:13 PM
Re: AddPrinterConnection -> does not work sometimes
[Re: ShaneEP]
|
ShaneEP
MM club member
   
Registered: 2002-11-29
Posts: 2127
Loc: Tulsa, OK
|
Give this a shot...
$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
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
0 registered
and 616 anonymous users online.
|
|
|