Page 1 of 2 12>
Topic Options
#126902 - 2004-09-22 10:18 AM Making the script more verbose
Testprs Offline
Fresh Scripter

Registered: 2004-09-02
Posts: 20
Loc: Netherlands, Delft
Hi,

I want to make our login script more verbose with a welcome message for the user and some more error messaging. One part of the scipt is the adding of printers on laptops and workstations in case they don't have them installed allready. This is the thing I use for it:

IF KeyExist("HKEY_CURRENT_USER\Printers\Connections\,,finasserver,2100_DLS")
? "Printer 2100_DLS allready added"
ELSE ADDPRINTERCONNECTION ("\\Finasserver\2100_DLS")
ENDIF

How could I make this more verbose using the @SERROR function?

would it be something like this:

IF KeyExist("HKEY_CURRENT_USER\Printers\Connections\,,finasserver,2100_DLS")
? "Printer 2100_DLS allready added"
ELSE
IF ADDPRINTERCONNECTION ("\\Finasserver\2100_DLS")=0
? "Printer succesfully added"
ELSE 'unable to add printer ' + @ERROR + ' ' + @SERRROR
ENDIF

regards,

jeroen

Top
#126903 - 2004-09-22 12:29 PM Re: Making the script more verbose
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
That's absolutely fine, though from a aesthetic view and for portability / ease of maintenance I'd prefer:

Code:
MapPrinter('\\Finasserver\2100_DLS')


Function MapPrinter($sPrinter)
Dim $asPrinterBits

$asPrinterBits=Split($sPrinterPath,"\")

If KeyExist("HKEY_CURRENT_USER\Printers\Connections\,,"+$asPrinterBits[2]+","+$asPrinterBits[3])
"Printer '"+$sPrinter+'" already present"+@CRLF
Else
If AddPrinterConnection($sPrinter)
"Cannot add printer '"+$sPrinter+"'"+@CRLF
"Reason: ["+@ERROR+"] "+@SERROR+@CRLF
Exit @ERROR
Else
"Printer '"+$sPrinter+"' succesfully added."+@CRLF
EndIf
EndIf

Exit 0

EndFunction


Top
#126904 - 2004-09-22 01:42 PM Re: Making the script more verbose
Testprs Offline
Fresh Scripter

Registered: 2004-09-02
Posts: 20
Loc: Netherlands, Delft
Hi,
thanks for the quick response. Could I then do it like this if I have a batch of printers to add (about 15 or 20) to make one file where I call the function and one file where I use the function to map all the printers?

regards,

jeroen



Top
#126905 - 2004-09-22 02:15 PM Re: Making the script more verbose
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Yes.

You can store the function (also called a UDF - User Defined Function) in an external file, or in your main file.

If for example you store the function in an external file called "MapPrinter.UDF" you would call the file at the start of your script to load the function:
Code:
CALL "\\server\path-to-files\MapPrinter.udf"



Now you can call the function as many times as you like:
Code:
If InGroup("Group1)

MapPrinter("\\server\printer1")
MapPrinter("\\Server\printer2")
EndIf
If InGroup("Groupx)
MapPrinter("\\server\printer2")
EndIf



Alternatively, just add the function to the end of your main script and call it in the same way.

Search the UDF forum for some more robust printer mapping UDFs, and have a look at the FAQ forum for information on how to use UDFs in your own scrips.

Top
#126906 - 2004-09-22 04:02 PM Re: Making the script more verbose
Testprs Offline
Fresh Scripter

Registered: 2004-09-02
Posts: 20
Loc: Netherlands, Delft
Hi,

If I call the MapPrinter.udf file the script gives the following error:

ERROR: missing ENDFUNCTION for [mapprinter]!

is it because the endfunction is stated after the EXIT 0?
(I'm kinda new to programming, so I hope you're not banging your head into the wall now with laughter... )

regards,

jeroen

Top
#126907 - 2004-09-22 04:08 PM Re: Making the script more verbose
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
no, it is not because of exit 0 ...
show us what you saved (contents of both the script and udf files)

P.S.: no laughter from here
_________________________



Top
#126908 - 2004-09-22 04:15 PM Re: Making the script more verbose
Testprs Offline
Fresh Scripter

Registered: 2004-09-02
Posts: 20
Loc: Netherlands, Delft
MapPrinter.udf:
Code:

Function MapPrinter($sPrinter)
Dim $asPrinterBits

$asPrinterBits=Split($sPrinterPath,"\")

If KeyExist("HKEY_CURRENT_USER\Printers\Connections\,,"+$asPrinterBits[2]+","+$asPrinterBits[3])
"Printer '"+$sPrinter+'" already present"+@CRLF
Else
If AddPrinterConnection($sPrinter)
"Cannot add printer '"+$sPrinter+"'"+@CRLF
"Reason: ["+@ERROR+"] "+@SERROR+@CRLF
Exit @ERROR
Else
"Printer '"+$sPrinter+"' succesfully added."+@CRLF
EndIf
EndIf

Exit 0

EndFunction



Addprinter.kix:
Code:
 
Call "%LOGONSERVER%\NETLOGON\MapPrinter.udf"

;add printers
MapPrinter("\\Finasserver\2100_DLS")
MapPrinter("\\Finasserver\2100_DLS_2")
MapPrinter("\\Finasserver\2100_Victor")
MapPrinter("\\Finasserver\2200_Aflev")
MapPrinter("\\Finasserver\2200_Planning")
MapPrinter("\\Finasserver\2200_CTR")
MapPrinter("\\Finasserver\2200_DTS")
MapPrinter("\\Finasserver\2200_HR")
MapPrinter("\\Finasserver\2200_Logistiek")
MapPrinter("\\Finasserver\2200_Magazijn")
MapPrinter("\\Finasserver\2200_receptie")
MapPrinter("\\Finasserver\2200_Sales")
MapPrinter("\\Finasserver\2200_Secretariaat")
MapPrinter("\\Finasserver\2200_SLS")
MapPrinter("\\Finasserver\2200_Tanja")
MapPrinter("\\Finasserver\2300_Automatisering")
MapPrinter("\\Finasserver\2300_Inkoop")
MapPrinter("\\Finasserver\2300_Planning")
MapPrinter("\\Finasserver\2300_DLS")
MapPrinter("\\Finasserver\2300_Warehousing")
MapPrinter("\\Finasserver\4000_Order")
MapPrinter("\\Finasserver\4100_DLS")
MapPrinter("\\Finasserver\4100_DLS_landscape")
MapPrinter("\\Finasserver\4100_Finan")
MapPrinter("\\Finasserver\4450C_Sales")




regards,

jeroen

Top
#126909 - 2004-09-22 04:32 PM Re: Making the script more verbose
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
there is at least one quote mismatch in MapPrinter.udf (didn't bother to check others )

"Printer '"+$sPrinter+'" already present"+@CRLF

should be

"Printer '"+$sPrinter+"' already present"+@CRLF


that should cause the error

P.S.: still not laughing ...


Edited by Jochen (2004-09-22 04:34 PM)
_________________________



Top
#126910 - 2004-09-22 04:58 PM Re: Making the script more verbose
Testprs Offline
Fresh Scripter

Registered: 2004-09-02
Posts: 20
Loc: Netherlands, Delft
thanks for the help (and for not laughing! ). It was the solution. But... now there's a new message:
ERROR : array reference out of bounds!
Line: 6

regards,

jeroen

Top
#126911 - 2004-09-22 05:08 PM Re: Making the script more verbose
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Oops. My bad.

I typed the script straight into the post, and a couple of typos have crept in where I converted it from inline code to a UDF as I was typing.

change this line:
Code:
$asPrinterBits=Split($sPrinterPath,"\")



to:
Code:
$asPrinterBits=Split($sPrinter,"\")



Top
#126912 - 2004-09-23 10:27 AM Re: Making the script more verbose
Testprs Offline
Fresh Scripter

Registered: 2004-09-02
Posts: 20
Loc: Netherlands, Delft
Thanks... works great now...
now there is this other part that I would like to make the following part more verbose for troubleshooting:

Code:

;Toevoegen Bureaublad Distri Logo en NTB Screensaver
$Filename = Dir("C:\program files\Distri\")
If @ERROR <> 0
MD "C:\Program files\Distri"
EndIf
Copy "\\MARS\C_Profiles$\distri\latitude.bmp" "c:\program files\distri\latitude.bmp"
Copy "\\MARS\C_Profiles$\distri\Telecombon.scr" "c:\program files\distri\Telecombon.scr"
SetFileAttr ("c:\program files\distri", 32)
SetFileAttr ("c:\program files\distri\Telecombon.scr", 32)
WriteValue("HKEY_CURRENT_USER\Control Panel\Desktop","WallPaperStyle","0","REG_SZ")
WriteValue("HKEY_CURRENT_USER\Control Panel\Desktop","TileWallPaper","0","REG_SZ")
WriteValue("HKEY_CURRENT_USER\Control Panel\Colors","Background","58 110 165","REG_SZ")
SetWallpaper ("C:\progra~1\distri\latitude.bmp",1)



Should set the wallpaper, but doesn't allways work.


Top
#126913 - 2004-09-23 11:23 AM Re: Making the script more verbose
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Functions like WriteValue() and SetWallpaper will return a value indicating success (0) or error (<>0). They will also set @ERROR.

Commands like COPY will set @ERROR to 0 to indicate success, and <>0 for an error.

You need to check these values after each call, and display a message.

Here is one way to do it:
Code:
;Toevoegen Bureaublad Distri Logo en NTB Screensaver
$Filename = Dir("C:\program files\Distri\")
If @ERROR <> 0
MD "C:\Program files\Distri"
ShowStatus("Creating distri directory",1)
EndIf
Copy "\\MARS\C_Profiles$\distri\latitude.bmp" "c:\program files\distri\latitude.bmp"
ShowStatus("Copying latitute.bmp")
Copy "\\MARS\C_Profiles$\distri\Telecombon.scr" "c:\program files\distri\Telecombon.scr"
ShowStatus("Copying telcombon.scr")
$=SetFileAttr("c:\program files\distri", 32)
ShowStatus("Setting attributes on distri directory")
$=SetFileAttr("c:\program files\distri\Telecombon.scr", 32)
ShowStatus("Setting attributes on telecombon.scr")
$=WriteValue("HKEY_CURRENT_USER\Control Panel\Desktop","WallPaperStyle","0","REG_SZ")
ShowStatus("Setting wallpaperstyle")
$=WriteValue("HKEY_CURRENT_USER\Control Panel\Desktop","TileWallPaper","0","REG_SZ")
ShowStatus("Setting tilewallpaper")
$=WriteValue("HKEY_CURRENT_USER\Control Panel\Colors","Background","58 110 165","REG_SZ")
ShowStatus("Setting background colors")
$=SetWallpaper("C:\progra~1\distri\latitude.bmp",1)
ShowStatus("Setting wallpaper",1)


; ShowStatus()
Function ShowStatus($sMessage, Optional $bVerbose)
If @ERROR
"ERROR: "+$sMessage+@CRLF
"Reason: ["+@ERROR+"] "+@SERROR+@CRLF
Else
If $bVerbose "SUCCESS: "+$sMessage+@CRLF EndIf
EndIf
Exit @ERROR
EndFunction



Note, the second parameter to the function $bVerbose is optional. If not present (or zero) it will only report errors. If it is set to true it will report both errors and successes.

Top
#126914 - 2004-09-23 03:04 PM Re: Making the script more verbose
Testprs Offline
Fresh Scripter

Registered: 2004-09-02
Posts: 20
Loc: Netherlands, Delft
Hi,

thanks for all the help. Going better and better now! now there's the following:

I've tried a bit of creative copy pasting (from the Maprinter.udf) and created the NETUSE.udf file:
Code:
  
; NETUSE()
Function NETUSE($nDrive, $nUse, Optional $bVerbose)

If USE $nDrive +$nUse
"Kan schijf '"+$nDrive+"' niet verbinden"+@CRLF
"Reden: ["+@ERROR+"] "+@SERROR+@CRLF
Exit @ERROR
Else
"Schijf '"+$nDrive+"' succesvol verbonden met '"+$nUse+"."+@CRLF
EndIf

Exit @ERROR
EndFunction



but it gives me an error message:

Error in expression.

Now I had a couple of questions:

- what am I doing wrong?
- is there a reason to specify a variable with the small caption first ($sPrinter) ?
- why isn't there a argument after "If USE $nDrive +$nUse
"?

regards,

jeroen

Top
#126915 - 2004-09-23 03:16 PM Re: Making the script more verbose
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
USE is a command, not a function and as such does not return anything to the IF construct.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#126916 - 2004-09-23 03:32 PM Re: Making the script more verbose
Testprs Offline
Fresh Scripter

Registered: 2004-09-02
Posts: 20
Loc: Netherlands, Delft
then how could I fix this? I want to make a udf that maps the drive and if succesfull tells me this and if not succesfull tells me this and why.

regards,

jeroen

Top
#126917 - 2004-09-23 03:33 PM Re: Making the script more verbose
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Quote:

- what am I doing wrong?



As Les says, USE is a command. Commands do not return anything, but they do set the @ERROR macro. Because they do not return anything thay cannot be used in an expression. The IF construct is expecting an expression.

Functions do return a value (and set @ERROR) so can be used in a conditional construct such as IF and WHILE.

In this case you need to write your code as:
Code:
USE X: \\server\share
If @ERROR
"ERROR" ?
Else
"SUCCESS" ?
EndIf



Quote:

- is there a reason to specify a variable with the small caption first ($sPrinter) ?



It is good programming practice to make your variable names reflect their use. This is known as "Hungarian Notation". In this case we use a simplified style "Short Form Hungarian Notation", and the "s" just means that the variable will contain string values. It's not so important in KiXtart as variables are not declared with a type, but it does help to keep your code easy to read and debug. Search the web if you want to know more about Hungarian Notation.

Quote:

- why isn't there a argument after "If USE $nDrive +$nUse




IF takes an expression which must evaluate to true or false. In the case of USE it won't work because USE does not return a value.

For something like WriteValue() which does return a value you can check it in an expression. It will return a zero value when there is no error, or an error code when there is an error.

This means that these two statements are exactly the same:
Code:
If WriteValue(a,b,c)
"ERROR" ?
EndIf
... and ...
If WriteValue(a,b,c) <> 0
"ERROR" ?
EndIf



You can reverse the test using Not, so
Code:
If WriteValue(a,b,c)=0
"SUCCESS" ?
EndIf
... is the same as ...
If Not WriteValue(a,b,c)
"SUCCESS" ?
EndIf




Top
#126918 - 2004-09-23 03:58 PM Re: Making the script more verbose
Testprs Offline
Fresh Scripter

Registered: 2004-09-02
Posts: 20
Loc: Netherlands, Delft
So it should be like this:
Code:
 
; NETUSE()
Function NETUSE($nDrive, $nUse, Optional $bVerbose)

USE $nDrive +$nUse
If @ERROR
"Kan schijf '"+$nDrive+"' niet verbinden"+@CRLF
"Reden: ["+@ERROR+"] "+@SERROR+@CRLF
Exit @ERROR
Else
"Schijf '"+$nDrive+"' succesvol verbonden met '"+$nUse+"."+@CRLF
EndIf

Exit @ERROR
EndFunction



the NETUSE.udf i mean...???

regards,

jeroen

Top
#126919 - 2004-09-23 04:24 PM Re: Making the script more verbose
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Quote:

So it should be like this:




Sure, but you have the ShowStatus() UDF we defined earlier so why not use it? There is no point is typing the same code in many times:

Code:
; NETUSE()

Function NETUSE($nDrive, $nUse, Optional $bVerbose)
USE $nDrive $nUse
ShowStatus("Mapping "+$sDrive+" to "+$nUse,$bVerbose)
Exit @ERROR
EndFunction



PS. You don't need to keep a single UDF in a file, you can add more than one to the file and load it as a library using a single CALL.

Top
#126920 - 2004-09-24 09:57 AM Re: Making the script more verbose
Testprs Offline
Fresh Scripter

Registered: 2004-09-02
Posts: 20
Loc: Netherlands, Delft
Thanks again! now's here's a new one. I'm trying to set the default printer depending on the group there in. I've created groups in the AD having the following format: P-<printername>. So I thought I could take this info from the membership and strip the P- part and use the rest to set the defaultprinter. So i've "made" this script:
Code:

;ShowStatusPrinter()
Function ShowStatusPrinter($sStatus)
Dim $asGroupBits
Dim $asGroups
Dim $sPrintserver
$asGroups=EnumGroup(@USERID)
$asGroupBits=Split($asGroups,"P-")
$sPrintserver = "\\FINASSERVER\"

If $sStatus = 1
? ""+$sPrintserver +$asGroupBits""
EndIf

EndFunction


The ? part is just to test if the correct info is used in the script, but I'm getting the message:
ERROR : error in expression: this type af array is not supported in expressions.!

regards,

jeroen

Top
#126921 - 2004-09-24 10:28 AM Re: Making the script more verbose
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
I'm not surprised, just about every line of your script is wrong.

Please study the manual and the samples we've provided so far. If you don't understand what the functions are doing then ask us to explain. At the moment code that you are producing is so badly formed that you are never going to be able to write scripts yourself.

Here is some code that will do what you need:
Code:
$sPrinter=GetDefaultPrinter()
If $sPrinter
"Default printer for this user is "+$sPrinter+@CRLF
Else
"No default printer found for this user."+@DRLF
EndIf

; GetDefaultPrinter() - Returns the default printer.
; Sets @ERROR=0 when a printer is found, 2 if no printer is found.
Function GetDefaultPrinter()
Dim $iIndex,$sGroup

$sGroup=EnumGroup($iIndex)
While @ERROR=0 AND $sGroup
If InStr($sGroup,"\") $sGroup=SubStr($sGroup,InStr($sGroup,"\")+1) EndIf
If InStr($sGroup,"P-")=1
$GetDefaultPrinter=SubStr($sGroup,3)
Exit 0
EndIf
$iIndex=$iIndex+1
$sGroup=EnumGroup($iIndex)
Loop
Exit 2
EndFunction



Before you move on, ensure that you understand how this function works.

Specifically, ensure you understand
  • Why there is a While...Loop construct
  • Why your use of @USERID was inappropriate
  • What the $iIndex variable is being used for
  • Why the line with the "\" substring is there
  • What the $GetDefaultPrinter variable is, and why it is set as it is.
  • What the "Exit" statements are being used for, and why they have numeric values
  • What the line "While @ERROR=0 AND $sGroup" is doing.

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 (Allen) and 641 anonymous users online.
Newest Members
batdk82, StuTheCoder, M_Moore, BeeEm, min_seow
17885 Registered Users

Generated in 0.076 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