#203193 - 2011-10-01 06:00 PM
Map Network Drive Base on INI File & Group
|
Kevin4u2005
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
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!
|
|
Top
|
|
|
|
#203195 - 2011-10-01 06:16 PM
Re: Map Network Drive Base on INI File
[Re: Glenn Barnas]
|
Kevin4u2005
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
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[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 SCRIPTBreak 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
|
|
|
|
#203202 - 2011-10-03 04:26 PM
Re: Map Network Drive Base on INI File
[Re: Les]
|
ShaneEP
MM club member
   
Registered: 2002-11-29
Posts: 2127
Loc: Tulsa, OK
|
Oh yeah, thanks for noticing that Brad, totally slipped my mind.
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
|
|
|
|
#203265 - 2011-10-15 05:59 PM
Re: Map Network Drive Base on INI File
[Re: Björn]
|
Kevin4u2005
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
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...
$inifile = @LServer+"\netlogon\inifile.ini"
Edited by ShaneEP (2011-10-15 06:58 PM)
|
|
Top
|
|
|
|
#203272 - 2011-10-16 05:42 AM
Re: Map Network Drive Base on INI File
[Re: Les]
|
Kevin4u2005
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
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
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
1 registered
(Allen)
and 1198 anonymous users online.
|
|
|