Page 1 of 2 12>
Topic Options
#203193 - 2011-10-01 06:00 PM Map Network Drive Base on INI File & Group
Kevin4u2005 Offline
Fresh Scripter

Registered: 2009-03-05
Posts: 5
Loc: Hong Kong
Hi I did write some kind of simple Kix script in the past but almost forget everything since then I never written anything but now I need to write again. Can someone please help with this? I want to map a network drive by using ini file which contain the following information. ; Map Drive For Everyone [Domain Users] S: "\\fileserver\software" [Finance] O: "\\fileserver\finance" [Marketing] M: “\\fileserver\marketing” I can map the drive by using the follow script but I don’t want to change the script every time whenever I need to insert the new network drive for user. Therefore I just want share the folder on the network and insert group name [groupname] and the path to share in the ini file as you can see the above. The next time when the script run it automatically map the new network for users If InGroup (Domain Users) = 1 Use G: "\\ldcgoidc01\Fileshare" /PERSISTENT ? "G File share Drive mapped successful" Else ? "G Drive Mapping unsuccessful. Please contact IT " Endif Or Select case Ingroup("Finance") Use H: "\\fileserver\Finance" /PERSISTENT ? "H Finance Drive Mapped Successful" chr (10) case InGroup ("Marketing") Use M: "\\fileserver\Marketing" /PERSISTENT ? "M Marketing Drive Mapped Successful" chr (10) Case 1 EndSelect I don’t know how to write a Function to do this. So I hope someone can help me with this. Thank you in Advance...

Edited by Kevin4u2005 (2011-10-01 06:08 PM)

Top
#203194 - 2011-10-01 06:08 PM Re: Map Network Drive Base on INI File [Re: Kevin4u2005]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4401
Loc: New Jersey
Welcome to KORG!

If you're not comfortable writing this, you can download the Universal Login Script from my web site - see the Products / Admin Toolchest section. It's Kix-based, uses an INI file to define resources, can map drives & printers, display user messages, and run commands all using common authorization logic based on groups, users, computers, AD sites, subnets, and even perform lookups based on most of these. The script supports 6 languages natively and additional languages can be supported by simply creating a message file.

We offer free and commercial support, too.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#203195 - 2011-10-01 06:16 PM Re: Map Network Drive Base on INI File [Re: Glenn Barnas]
Kevin4u2005 Offline
Fresh Scripter

Registered: 2009-03-05
Posts: 5
Loc: Hong Kong
Thanks Glenn but I really want to know how it can be written based on the above situation
Top
#203196 - 2011-10-01 07:44 PM Re: Map Network Drive Base on INI File [Re: Kevin4u2005]
ShaneEP Moderator Offline
MM club member
*****

Registered: 2002-11-29
Posts: 2127
Loc: Tulsa, OK
Here is a quick example that may or may not completely serve your needs, but it should get you started either way.

INI FILE
 Code:
[EVERYONE]
group=Domain Users
drive=S:
share=\\fileserver\software

[FINANCE]
group=Finance
drive=O:
share=\\fileserver\finance

[MARKETING]
group=Marketing
drive=M:
share=\\fileserver\marketing

KIX SCRIPT
 Code:
Break On					;;; ALLOWS SCRIPT TO BE INTERRUPTED DURING EXECUTION (may want to remove once in production)
$nul = SetOption("NoVarsInStrings","On")	;;; IGNORES VARIABLES THAT ARE INCLUDED IN STRINGS
$nul = SetOption("NoMacrosInStrings","On")	;;; IGNORES MACROS THAT ARE INCLUDED IN STRINGS
$nul = SetOption("Explicit","On")		;;; REQUIRES ALL VARIABLES TO BE DECLARED

Dim $inifile,$mappings,$mapping,$group,		;;;  DIM VARIABLES
    $drive,$share,$

$inifile = "\\path\to\inifile.ini"		;;; CHANGE THIS TO THE CORRECT PATH OF YOUR DRIVE INI FILE

$mappings = EnumINI($inifile)			;;; USES EnumINI() UDF TO CREATE AN ARRAY LIST OF ALL THE SECTIONS IN INI FILE

For Each $mapping in $mappings			;;; CYCLES THROUGH EACH ELEMENT OF THE ARRAY

   $group = ReadProfileString($inifile,$mapping,"group")	;;; READS THE GROUP KEY FROM CURRENT SECTION
   $drive = ReadProfileString($inifile,$mapping,"drive")	;;; READS THE DRIVE KEY FROM CURRENT SECTION
   $share = ReadProfileString($inifile,$mapping,"share")	;;; READS THE SHARE KEY FROM CURRENT SECTION

   ? "Mapping "+$group+" to the "+$share+" share using drive letter "+$drive

   Use $drive $share /persistent		;;; ATTEMPTS TO CONNECT DRIVE
   If @Error=0					;;; CHECKS FOR ERROR
      ? "Mapped successfully"
   Else
      ? "Error"
   Endif
Next

? "Script complete. Press any key to exit..."
get $						;;; PAUSES SCRIPT UNTIL KEY IS PRESSED

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;  EnumINI() - Enumerates INI file keys into an array.                               ;;;
;;;  Written By: Glenn Barnas                                                          ;;;
;;;  http://www.kixtart.org/forums/ubbthreads.php?ubb=showflat&Board=7&Number=135385   ;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Function EnumIni($_fSrcFile, OPTIONAL $_fSectName)
  Dim $_fSectList
  If Exist($_fSrcFile) = 0
    Exit 2
  EndIf
  $_fSectList = ReadProfileString($_fSrcFile, $_fSectName, '')
  If @ERROR
    Exit @ERROR
  EndIf
  If Len($_fSectList) > 0
    $EnumIni = Split(Left($_fSectList,len($_fSectList)-1), Chr(10))
    Exit 0
  EndIF
  Exit 13
EndFunction


Edited by ShaneEP (2011-10-01 07:55 PM)
Edit Reason: added comments to script

Top
#203198 - 2011-10-03 11:51 AM Re: Map Network Drive Base on INI File [Re: ShaneEP]
BradV Offline
Seasoned Scripter
****

Registered: 2006-08-16
Posts: 687
Loc: Maryland, USA
I think you will probably want to add some tests for group membership before mapping a share. For example, you probably only want to give access to the finance share to those people in the finance group? So, you can take Shane's example above and add a test for group membership before deciding whether to map or not.
Top
#203200 - 2011-10-03 03:04 PM Re: Map Network Drive Base on INI File [Re: BradV]
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
Mapping drives based on group membership can be fraught with problems. Confusion reins when my H: is different than your H:

Sooner or later, someone will be a member of two or more groups and their drive mapping conflict. Using alternate drive letters to mitigate the conflict only causes more problems when my H: is your K:. Embedded links may break as well.

You can decomplicate the path with DFS and not use drive letters for everything, relying instead on UNC. You can map a drive letter to a common root (or DFS) entry point and count on NTFS ACL to give access.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#203202 - 2011-10-03 04:26 PM Re: Map Network Drive Base on INI File [Re: Les]
ShaneEP Moderator Offline
MM club member
*****

Registered: 2002-11-29
Posts: 2127
Loc: Tulsa, OK
Oh yeah, thanks for noticing that Brad, totally slipped my mind.

 Code:
Break On					;;; ALLOWS SCRIPT TO BE INTERRUPTED DURING EXECUTION (may want to remove once in production)
$nul = SetOption("NoVarsInStrings","On")	;;; IGNORES VARIABLES THAT ARE INCLUDED IN STRINGS
$nul = SetOption("NoMacrosInStrings","On")	;;; IGNORES MACROS THAT ARE INCLUDED IN STRINGS
$nul = SetOption("Explicit","On")		;;; REQUIRES ALL VARIABLES TO BE DECLARED

Dim $inifile,$mappings,$mapping,$group,		;;;  DIM VARIABLES
    $drive,$share,$

$inifile = "\\path\to\inifile.ini"		;;; CHANGE THIS TO THE CORRECT PATH OF YOUR DRIVE INI FILE

$mappings = EnumINI($inifile)			;;; USES EnumINI() UDF TO CREATE AN ARRAY LIST OF ALL THE SECTIONS IN INI FILE

For Each $mapping in $mappings			;;; CYCLES THROUGH EACH ELEMENT OF THE ARRAY

   $group = ReadProfileString($inifile,$mapping,"group")	;;; READS THE GROUP KEY FROM CURRENT SECTION
   $drive = ReadProfileString($inifile,$mapping,"drive")	;;; READS THE DRIVE KEY FROM CURRENT SECTION
   $share = ReadProfileString($inifile,$mapping,"share")	;;; READS THE SHARE KEY FROM CURRENT SECTION

   If InGroup($group)
      ? "Mapping "+$group+" to the "+$share+" share using drive letter "+$drive

      Use $drive $share /persistent		;;; ATTEMPTS TO CONNECT DRIVE
      If @Error=0				;;; CHECKS FOR ERROR
         ? "Mapped successfully"
      Else
         ? "Error"
      Endif
   Endif
Next

? "Script complete. Press any key to exit..."
get $						;;; PAUSES SCRIPT UNTIL KEY IS PRESSED

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;  EnumINI() - Enumerates INI file keys into an array.                               ;;;
;;;  Written By: Glenn Barnas                                                          ;;;
;;;  http://www.kixtart.org/forums/ubbthreads.php?ubb=showflat&Board=7&Number=135385   ;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Function EnumIni($_fSrcFile, OPTIONAL $_fSectName)
  Dim $_fSectList
  If Exist($_fSrcFile) = 0
    Exit 2
  EndIf
  $_fSectList = ReadProfileString($_fSrcFile, $_fSectName, '')
  If @ERROR
    Exit @ERROR
  EndIf
  If Len($_fSectList) > 0
    $EnumIni = Split(Left($_fSectList,len($_fSectList)-1), Chr(10))
    Exit 0
  EndIF
  Exit 13
EndFunction

Top
#203255 - 2011-10-14 10:51 AM Re: Map Network Drive Base on INI File [Re: ShaneEP]
Björn Offline
Korg Regular
*****

Registered: 2005-12-07
Posts: 953
Loc: Stockholm, Sweden.
Hi and welcome!

You're welcome to look at another approach with a different layout for the 'ini' (or, reference-file) in this thread :
http://www.kixtart.org/forums/ubbthreads.php?ubb=showflat&Main=24889&Number=202966
_________________________
as long as it works - why fix it?
If it doesn't work - kix-it!

Top
#203265 - 2011-10-15 05:59 PM Re: Map Network Drive Base on INI File [Re: Björn]
Kevin4u2005 Offline
Fresh Scripter

Registered: 2009-03-05
Posts: 5
Loc: Hong Kong
Thank you so much ShaneEP for the script. I have tried this and it's working but only one problem when I put @LServer in the path than it doesn't works.
$inifile = "\\@LServer\netlogon\inifile.ini"
If I change the path as
$inifile = "\\fileserver\netlogon\inifile.ini"
then it works perfectly but I don't want to put server name directly as we have so many servers (domain controller) and if this server go offline then the script will not be able to map the drive. Any idea why it's not working and is there any alternative way.

Top
#203266 - 2011-10-15 06:56 PM Re: Map Network Drive Base on INI File [Re: Kevin4u2005]
ShaneEP Moderator Offline
MM club member
*****

Registered: 2002-11-29
Posts: 2127
Loc: Tulsa, OK
It could be because one of the first lines in that script sets the NoMacrosInStrings to ON, which means that macros (@'s) are ignored and treated as normal text when enclosed in ""s. It could also be because the @LServer returns \\ along with the servername, so when you put two slashes before it, it resolves as \\\\server instead of \\server.

Try this instead and it should fix your problem...

 Code:
$inifile = @LServer+"\netlogon\inifile.ini"


Edited by ShaneEP (2011-10-15 06:58 PM)

Top
#203270 - 2011-10-15 08:27 PM Re: Map Network Drive Base on INI File [Re: ShaneEP]
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
No need to either hard code a servername nor to use @LServer.

$inifile = "\\domain.tld\netlogon\inifile.ini"
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#203272 - 2011-10-16 05:42 AM Re: Map Network Drive Base on INI File [Re: Les]
Kevin4u2005 Offline
Fresh Scripter

Registered: 2009-03-05
Posts: 5
Loc: Hong Kong
ShaneEP & Les both of your method works. Thank You so much for the help. I really appreciated.
Top
#204107 - 2012-01-24 03:17 PM Re: Map Network Drive Base on INI File [Re: Kevin4u2005]
Kevin4u2005 Offline
Fresh Scripter

Registered: 2009-03-05
Posts: 5
Loc: Hong Kong
Hi
I am facing a problem with this script a bit for private share drive. For example I have shared a folder called Backup and underneath I have created subfolders for each user same as their logon name. I have applied read permission on Backup share (both share + NTFS) and subfolder full control to appropriate users (NTFS) for their concerned folder. Now the problem is when the script try to run it give out error message as follow.

The network name cannot be found (67).

Here is the ini file information.
[Private Share]
group=private
drive=P:
share=\\fildserver\Backup\ @userid

It seems like it can’t resolve @userid macro to proper user logon name as when I modify parameter in ini as follow then it worked

[Private Share]
group=private
drive=P:
share=\\fileserver\Backup\dkidd (user login)

I don’t want to use user logon name in ini file instead I want it to resolve @userid macro to get user logon for mapping the drive. I have tried with and without the follow part but the result is same.
$nul = SetOption("NoVarsInStrings","On") ;;; IGNORES VARIABLES THAT ARE INCLUDED IN STRINGS
;$nul = SetOption("NoMacrosInStrings","On") ;;; IGNORES MACROS THAT ARE INCLUDED IN STRINGS
$nul = SetOption("Explicit","On") ;;; REQUIRES ALL VARIABLES TO BE DECLAR

by the way, is there any way to modify the script to show share name to Private share instead of Private share on fileserver for mapped drive on client machine.

I hope someone can help me with these two issues.

Thanks

Top
#204108 - 2012-01-24 03:28 PM Re: Map Network Drive Base on INI File [Re: Kevin4u2005]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
You can leave out the @userid in the INI file and add it in the script.
You can change the name of the drive with the Label() UDF. Both answers are included in the example below.

 Code:
$privateshare = ReadProfileString("inifile.ini", "Private Share", "Share")

$privateshare = $privateshare + @USERID

Use x: $privateshare
Label('X:\', 'Private share')


;Below is a UDF. A UDF is ready for use and should not be modifued.
;
;FUNCTION	Label()
;
;AUTHOR		Lonkero
;
;ACTION		Read and Write Drive Label
;
;SYNTAX		Label("DRIVE","LABEL")
;
;PARAMETERS
;		DRIVE
;		 - drive to rename
;		LABEL
;		 - new name of the drive
;		   to read the label, leave out
;
;RETURNS
;		nothing on write.
;		on read the current label.
;		check @error for possible errors
;
;DEPENDENCIES
;		Minimum operating systems: Windows 2000/ME
;
;EXAMPLE
;		"Current Label for C: is:"
;		Label('c:')
;		? "Changing it..."
;		Label('c:\','my fancy new name')
;		if @error
;		 ? "ERROR OCCURED:" @serror
;		else
;		 ? "New Label for C: is:"
;		 Label('c:')
;		endif
;
;CODE
Function label($_d, optional $_s)
	$ = CreateObject("shell.application")
	If VarType($_s)
		$ .namespace(Left($_d, 1) + ":\").self.name = $_s
		Exit @error
	Else
		$label = $ .namespace(Left($_d, 1) + ":\").self.name
		Exit @error
	EndIf
EndFunction
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#204109 - 2012-01-24 04:01 PM Re: Map Network Drive Base on INI File [Re: Kevin4u2005]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4401
Loc: New Jersey
You can't place Kix macros in an external file and expect them to be processed. You need to determine if the string contains a macro and then use something like the Execute command to evaluate the string and any macros that may be present. It's not related to SetOption values, either, so put those back to normal.

Here's a basic example:
 Code:
$Share = ReadProfileString($IniFile, $Section, 'Share']
If InStr($Share, '@')
 $Root  = Split($Share, '@')[0]
 $Macro = '@' + Split($Share, '@')[1]
 $ = Execute('$$Macro = $Macro')
 $Share = $Root + $Macro 
EndIf

'Use D: ' Share?
The Execute evaluates the string containing the macro and returns the result. Note that you'll need slightly more complex code if the macro is not at the end of the path.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#204114 - 2012-01-26 12:46 AM Re: Map Network Drive Base on INI File [Re: Glenn Barnas]
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
what would be the caveat using simply:
 Code:
 $ = Execute('$$Share = ' + ReadProfileString($IniFile, $Section, 'Share'))
 'Use D: ' $Share?
_________________________
!

download KiXnet

Top
#204115 - 2012-01-26 03:18 AM Re: Map Network Drive Base on INI File [Re: Lonkero]
Allen Administrator Online   shocked
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4562
Loc: USA
Spaces in the path will through it off.
Top
#204120 - 2012-01-26 03:56 PM Re: Map Network Drive Base on INI File [Re: Lonkero]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4401
Loc: New Jersey
I tried that and it did not work for me (Kix 4.62). Placing just the macro in the RValue worked, but if it was part of a string, it did not perform the assignment. The Execute instead displayed the macro on the screen and assigned the string text up to the macro to the var.
 Code:
Break On
$ = SetOption('NoMacrosInStrings', 'On')

$Share = '\\server\share\@USERID'
'Share: ' $Share ? ?
$ = Execute('$$Share = $Share')
?
'Use D: ' $Share ?
results in
 Code:
Share: \\server\share\@USERID

First Last
Use D: \\server\share\
Placing just the macro in the RVal in the Execute expands the macro and performs the assignment as expected. Turning the SetOption off causes the execute to choke when it encounters spaces in the string, as Allen mentioned. Usernames here are "First Last".

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#204124 - 2012-01-26 06:46 PM Re: Map Network Drive Base on INI File [Re: Glenn Barnas]
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
Glenn, your execute line also differs from mine.
_________________________
!

download KiXnet

Top
#204125 - 2012-01-26 06:47 PM Re: Map Network Drive Base on INI File [Re: Lonkero]
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
with your way the code would be:
 Code:
Break On
$ = SetOption('NoMacrosInStrings', 'On')

$Share = '\\server\share\@USERID'
'Share: ' $Share ? ?
$ = Execute('$$Share = '+$Share)
?
'Use D: ' $Share ?
_________________________
!

download KiXnet

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 1198 anonymous users online.
Newest Members
M_Moore, BeeEm, min_seow, Audio, Hoschi
17883 Registered Users

Generated in 0.076 seconds in which 0.027 seconds were spent on a total of 14 queries. Zlib compression enabled.

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