Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Need a sample script to map network drives based on group membership

Posted on 2007-11-15
11
Medium Priority
?
781 Views
Last Modified: 2012-08-13
I am looking for a sample script to map network drives for users based on their membership
Lets say the security groups that I have are
1. ldrive
2. mdrive
3. vdrive
4. zdrive

the network shares are
1. \\server\legal
2. \\server\marketing
3. \\server\visual
4. \\server\budget

I need ONE script to apply to the top level OU so that depending on which group the user belogs to they get a certain drive letter

thanks in advance
0
Comment
Question by:vmamedov
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
11 Comments
 

Author Comment

by:vmamedov
ID: 20292299
In the same script I am looking for code to remove the network drives first, before mapping them.
0
 
LVL 33

Expert Comment

by:Jeroen Rosink
ID: 20292610
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 20293297
Or this, slightly more tuned to suit your needs:

'==============
Dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")

Dim objNetwork
Set objNetwork = CreateObject("WScript.Network")

Dim objWinntUser
Set objWinntUser = GetObject("WinNT://" & objNetwork.UserDomain & "/" & objNetwork.UserName & ",user")

Dim strGroupToCheck, strGroupShare, strDrive, strShare

' Specity the group name, a colon symbol, and the network share
' in this case, the drive letter is taken as the first letter of the group name
arrGroupShares = Array( _
      "ldrive;\\server\legal", _
      "mdrive;\\server\marketing", _
      "vdrive;\\server\visual", _
      "zdrive;\\server\budget" _
      )

For Each strGroupShare In arrGroupShares
      strGroupToCheck = Split(strGroupShare, ";")(0)
      strDrive = Left(strGroupToCheck, 1)
      strShare = Split(strGroupShare, ";")(1)
      If IsMemberOfGroup(objNetwork.UserDomain, objWinntUser, strGroupToCheck) = True Then
            'MsgBox "You are a member of " & strGroupToCheck
            If objFSO.DriveExists(strDrive) = True Then objNetwork.RemoveNetworkDrive strDrive, True, True
            objNetwork.MapNetworkDrive strDrive, strShare, True, True
      ElseIf IsMemberOfGroup(objNetwork.UserDomain, objWinntUser, strGroupToCheck) = False Then
            'MsgBox "You are NOT a member of " & strGroupToCheck
      ElseIf IsMemberOfGroup(objNetwork.UserDomain, objWinntUser, strGroupToCheck) = "Error" Then
            'MsgBox "There was no group found called " & strGroupToCheck
      End If      
Next

Function IsMemberOfGroup(strUserDomain, objUser, strGroup) 'the user is a member of a specified group
      IsMemberOfGroup = False
      Dim objGroup
      On Error Resume Next
      Set objGroup = GetObject("WinNT://" & strUserDomain & "/" & strGroup & ",group")
      If Err.Number Then
            IsMemberOfGroup = "Error"
      Else
            IsMemberOfGroup = objGroup.IsMember(objUser.ADsPath)
            'MsgBox objUser.ADsPath
      End If
End Function
'==============

Regards,

Rob.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:vmamedov
ID: 20294929
Rob, as always you come up with exactly what I am looking for.

just to double check, this script removed the drive letters first? then maps the ones in thescript?

also can you be more specific as to how to customize this script?  where do i specify which drive letter i want to use, where do i specify which group to look at, and also where to i specify which network share to map?

i also would like to get rid of all message boxes

again thanks in advance
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 2000 total points
ID: 20294981
OK, no problem.

Yes, the script will remove the network drive, but only the one that it is about to map, but if it does each turn (if the user is a member of multiple groups) then it will remove that if it exists.

In terms of where do you put which drive letter to use, seeing as you specified that your groups might be "ldrive", "mdrive", etc, I just automatically get the drive letter from the first letter of the group name, but I'll change that, because that's not very flexible at all.....

So, with the new code, to customise the group name, drive letter, and share path, modify this bit:
' Specity the group name, a semi-colon symbol, the drive letter, a semi-colon symbol, and the network share
arrGroupShares = Array( _
      "groupforL;L;\\server\legal", _
      "groupforM;M;\\server\marketing", _
      "groupforV;V;\\server\visual", _
      "groupforZ;Z;\\server\budget" _
      )

so it is listed as described.....I hope that's self-explanatory enough....

Oh, and the MsgBox statements are all commented out, so they won't run.

'==============
Dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")

Dim objNetwork
Set objNetwork = CreateObject("WScript.Network")

Dim objWinntUser
Set objWinntUser = GetObject("WinNT://" & objNetwork.UserDomain & "/" & objNetwork.UserName & ",user")

Dim strGroupToCheck, strGroupShare, strDrive, strShare

' Specity the group name, a semi-colon symbol, the drive letter, a semi-colon symbol, and the network share
arrGroupShares = Array( _
      "groupforL;L;\\server\legal", _
      "groupforM;M;\\server\marketing", _
      "groupforV;V;\\server\visual", _
      "groupforZ;Z;\\server\budget" _
      )

For Each strGroupShare In arrGroupShares
      strGroupToCheck = Split(strGroupShare, ";")(0)
      strDrive = Split(strGroupShare, ";")(1)
      If Right(strDrive, 1) <> ":" Then strDrive = strDrive & ":"
      strShare = Split(strGroupShare, ";")(2)
      If IsMemberOfGroup(objNetwork.UserDomain, objWinntUser, strGroupToCheck) = True Then
            'MsgBox "You are a member of " & strGroupToCheck
            If objFSO.DriveExists(strDrive) = True Then objNetwork.RemoveNetworkDrive strDrive, True, True
            objNetwork.MapNetworkDrive strDrive, strShare, True, True
      ElseIf IsMemberOfGroup(objNetwork.UserDomain, objWinntUser, strGroupToCheck) = False Then
            'MsgBox "You are NOT a member of " & strGroupToCheck
      ElseIf IsMemberOfGroup(objNetwork.UserDomain, objWinntUser, strGroupToCheck) = "Error" Then
            'MsgBox "There was no group found called " & strGroupToCheck
      End If      
Next

Function IsMemberOfGroup(strUserDomain, objUser, strGroup) 'the user is a member of a specified group
      IsMemberOfGroup = False
      Dim objGroup
      On Error Resume Next
      Set objGroup = GetObject("WinNT://" & strUserDomain & "/" & strGroup & ",group")
      If Err.Number Then
            IsMemberOfGroup = "Error"
      Else
            IsMemberOfGroup = objGroup.IsMember(objUser.ADsPath)
            'MsgBox objUser.ADsPath
      End If
End Function
'==============

Regards,

Rob.
0
 

Author Comment

by:vmamedov
ID: 20295047
I will try this out tomorrow and report back. You have been a great help

Thanks
0
 

Author Comment

by:vmamedov
ID: 20298684
Rob,

I have implemented the script but its not giving me the error below

---------------------------
Windows Script Host
---------------------------
Script:      C:\Documents and Settings\user\Desktop\mappingdrives.vbs
Line:      32
Char:      13
Error:      The specified network password is not correct.

Code:      80070056
Source:       WSHNetwork.MapNetworkDrive

---------------------------
OK  
---------------------------

My line 32 is this
            objNetwork.MapNetworkDrive strDrive, strShare, True, True

Thanks in advance
0
 
LVL 12

Expert Comment

by:chandru_sol
ID: 20303852
Change that line from

objNetwork.MapNetworkDrive strDrive, strShare, True, True

to

objNetwork.MapNetworkDrive strDrive, strShare, True
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 20308815
Oh you've posted a new question for this....oh well..... I'm wondering if you've got some sort of cached credentials to a network resource....

Please follow the instructions in this article:
http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/usercpl_manage_passwords.mspx

to check if you have any cached passwords, and remove those that are for the same resource as the server you are trying to map a drive to.....

Regards,

Rob.
0
 

Author Comment

by:vmamedov
ID: 20312431
thank you chandru/rob for all your help

chandru, your solution was it, it solved the problem

rob, thank you for the original script and all the modifications that you have done

i will give full credit to rob on this post and give chandru full credit on the other post that i have opened regarding the specific error message.

again thanks for your help

if you both dont mind i have opened a another post regarding a different issue, if you can take a look at http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_22970085.html
0
 

Author Closing Comment

by:vmamedov
ID: 31409423
rob was great and answered all my questions and the script worked.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article we want to have a look at the directory attributes which are used by Microsoft to store the so called Security Identifiers (SID). These SIDs plays an important role in delegating and granting permissions and in authentication of trus…
Welcome, welcome!  If you are new to the series and haven't been following along, please take a brief moment to review the first three installments: Part 1 (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/A_266-VBScri…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question