#175856 - 2007-05-01 03:57 PM
not all variables are used
|
vroedie
Fresh Scripter
Registered: 2006-10-05
Posts: 30
|
Hi,
I use a script to map network drives. Now some users have problems with the mappings. The problem is that not all variables are used, so sometimes mappings point at the wrong share.
I've used a central script to start other scripts and load UDF's.
here's the important part:
Break On
;Declare global variables
Global $NetLogon, $LogFldr, $DFS
$NetLogon = "\\hollands-midden.local\netlogon\"
$LogFldr = "C:\Temp\"
$DFS = "\\RBHM\DFS"
;Load Functions
Call udf.kix
Call drives.kix
then there's the UDF's that are used (with thanks to Richard H.):
;ShowStatus()
;Gives (error messages), use verbose 0 for errors only, verbose 1 for error and success messages and verbose 2 for success only
Function ShowStatus($sMessage, Optional $bVerbose)
If @ERROR
If $bVerbose <> 2
"ERROR: "+$sMessage+@CRLF
"Reason: ["+@ERROR+"] "+@SERROR+@CRLF
EndIf
Else
If $bVerbose
"SUCCESS: "+$sMessage+@CRLF
EndIf
EndIf
Exit @ERROR
EndFunction
;MapDrv()
;Maps drive, deletes old mapping if necessary and gives detailed information if necessary
Function MapDrv($Drv, $Shr)
USE $Drv $DFS + $Shr
ShowStatus("Mapping $Shr: ", 2)
IF @ERROR <> 0
USE $Drv /DELETE /PERSISTENT
USE $Drv $DFS + $DataShr
ShowStatus("Mapping $Shr: ", 1)
ENDIF
EndFunction
and (a part of) the drives.kix file:
Dim $HomeShr, $AppShr, $AfdShr
$HomeShr = "\Home" ;Leiden home share
$AppShr = "\Applicatie" ;Leiden Applicatie share
$AfdShr = "\Afdeling" ;Leiden Afdeling share
MapDrv("F:", $HomeShr)
MapDrv("I:", $AppShr)
MapDrv("J:", $AfdShr)
sometimes the script mappes all drives to \\RBHM\DFS.
any idea's? i'm kinda stunned...
regards,
Jeroen
Edited by Howard Bullock (2007-05-01 04:01 PM) Edit Reason: Corrected code tags
|
|
Top
|
|
|
|
#175857 - 2007-05-01 04:10 PM
Re: not all variables are used
[Re: vroedie]
|
Howard Bullock
KiX Supporter
   
Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
|
I do not see where you are properly calling the MapDrv() function for different drives.
The functions requires two inputs "MapDrv($Drv, $Shr)". But you reference a global variable in the USE statement inside the MapDrv UDF. That should not be there.
USE $Drv $DFS + $Shr
All share info should be passed to the UDF when called.
|
|
Top
|
|
|
|
#175858 - 2007-05-01 04:30 PM
Re: not all variables are used
[Re: Howard Bullock]
|
vroedie
Fresh Scripter
Registered: 2006-10-05
Posts: 30
|
Hi,
sorry, but I don't really understand what you mean.
I do not see where you are properly calling the MapDrv() function for different drives.
Do you mean that I'm using the function incorrectly?
But you reference a global variable in the USE statement inside the MapDrv UDF. That should not be there.
should global variables never be used in functions?
should I build the DFS info before sending it to the function?
regards,
jeroen
|
|
Top
|
|
|
|
#175861 - 2007-05-01 05:47 PM
Re: not all variables are used
[Re: vroedie]
|
Howard Bullock
KiX Supporter
   
Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
|
Somewhere in your script I should see: Dim $HomeShr, $AppShr, $AfdShr
$HomeShr = $DFS + "\Home" ;Leiden home share
$AppShr = $DFS + "\Applicatie" ;Leiden Applicatie share
$AfdShr = $DFS + "\Afdeling" ;Leiden Afdeling share
MapDrv("F:", $HomeShr)
MapDrv("I:", $AppShr)
MapDrv("J:", $AfdShr)
Function MapDrv($Drv, $Shr)
USE $Drv $Shr
ShowStatus("Mapping $Shr: ", 2)
IF @ERROR <> 0
USE $Drv /DELETE /PERSISTENT
USE $Drv $DFS + $DataShr
ShowStatus("Mapping $Shr: ", 1)
ENDIF
EndFunction
I have included my UDF for you to use as a comparison. Note that it requried additioanl UDFs WriteLog and WriteLog2 which are available in the UDF library. Function MapDrive($Drive, $Server, $Share)
Dim $Drive, $Server, $Share, $shell
Color c+/n
If $Drive<>"" and $Server<>"" and $Share<>""
$LogText="Connecting $Drive to \\$Server\$Share"
? $LogText
USE $Drive /Delete /Persistent
USE $Drive "\\$Server\$Share"
If @error=0
color g+/n
$x=" - Success"
"$x"
If val($DOS) >= 5
$shell=createobject("shell.application")
$shell.namespace($Drive+"\").self.name=$Share + " on '" + $Server + "'"
$shell = 0
Endif
Else
color r+/n
$x=" - Failed: Error @error"
"$x"
$ErrorState=1
Endif
WriteLog ($LogText + $x)
WriteLog2("%temp%\MapDrive.log",$LogText + $x,1)
Color w+/n
Else
WriteLog ("Function 'MapDrive' called with invalid parameters: '$Drive', '$Server', '$Share'")
Endif
Endfunction
|
|
Top
|
|
|
|
#175880 - 2007-05-02 08:49 AM
Re: not all variables are used
[Re: Howard Bullock]
|
vroedie
Fresh Scripter
Registered: 2006-10-05
Posts: 30
|
Sorry,
I still don't get it. Somewhere in your script I should see:
I did put this in my script, I just placed the UDF in another script. Or do you mean the "$DFS + " part?
but then why should I make my DFS variable global instead of just dimming it?
regards,
jeroen
|
|
Top
|
|
|
|
#175894 - 2007-05-02 02:14 PM
Re: not all variables are used
[Re: vroedie]
|
Howard Bullock
KiX Supporter
   
Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
|
When I first posted I did not see the function calls because you had place 3 code sections. Hence my refernce to I should see. Because I did not see it initially.
The main point I was trying to make: You should not reference global variables inside functions. It is bad form and as you saw can cause you issues that are harder to diagnose. Check your input parameters to validate that they contain the proper data.
|
|
Top
|
|
|
|
#175903 - 2007-05-02 04:47 PM
Re: not all variables are used
[Re: Howard Bullock]
|
vroedie
Fresh Scripter
Registered: 2006-10-05
Posts: 30
|
sorry, i'll post in one code section next time. 
thanks for the help. I've changed my code and everything seems to be working a whole lot better now...
regards,
jeroen
|
|
Top
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
1 registered
(mole)
and 1300 anonymous users online.
|
|
|