Link to home
Start Free TrialLog in
Avatar of JMMLAN
JMMLAN

asked on

Using a script to map multiple drives based on group membership

Good Afternoon,
I have a very limted knowledge of scrupting using VBS however I was able to piece together the below script but it will only map a single network drive

On Error Resume Next

Set objSysInfo = CreateObject("ADSystemInfo")
Set objNetwork = CreateObject("Wscript.Network")

strUserPath = "LDAP://" & objSysInfo.UserName
Set objUser = GetObject(strUserPath)

For Each strGroup in objUser.MemberOf
    strGroupPath = "LDAP://" & strGroup
    Set objGroup = GetObject(strGroupPath)
    strGroupName = objGroup.CN

    Select Case strGroupName
        Case "AOD Users"
            objNetwork.MapNetworkDrive "M:", "\\QPRCAOD01\AOD"
       
       
    End Select
Next

How can I expand on this so that it will map more than one drive>  For example I would like to map up to five different drive letters using one script.  One drive would map to M, one to S, one to T, and another to H.  A user who is a mamber for the drives M,S, and T would get those drives only but not the T abd H drives.

Any help would be appreciated
Avatar of Todd Gerbert
Todd Gerbert
Flag of United States of America image

Try taking out the "On Error Resume Next" and see if you get any useful error messages (I'm wondering if strGroupName is being set to some invalid value (like null, or not being set at all) on additional iterations of the For...Next loop).

You could also add some MsgBox statements to check on the values of things like strGroupName while the script is running.

Avatar of JMMLAN
JMMLAN

ASKER

The code above works perfectly for mapping a single drive however I do not know how to expand it to add additiaonal drives based on membership in a group.
I'm sorry, I misunderstood your question - what you've got so far is so close the complete I assumed you cut a chunk out for brevity's sake.

All you need to do is add additional Case clauses to the Select Case block.  In the example below I also forced strGroupName to lower case for the sake of comparison.
On Error Resume Next
 
Set objSysInfo = CreateObject("ADSystemInfo")
Set objNetwork = CreateObject("Wscript.Network")
 
strUserPath = "LDAP://" & objSysInfo.UserName
Set objUser = GetObject(strUserPath)
 
For Each strGroup in objUser.MemberOf
    strGroupPath = "LDAP://" & strGroup
    Set objGroup = GetObject(strGroupPath)
    strGroupName = LCase(objGroup.CN)
 
    Select Case strGroupName
        Case "aod users"
            objNetwork.MapNetworkDrive "M:", "\\QPRCAOD01\AOD"
        Case "group number 2"
            objNetwork.MapNetworkDrive "N:", "\\Server\Group2Share"
        Case "group 3"
            objNetwork.MapNetworkDrive "O:", "\\Server\3rdShare"
    End Select
Next

Open in new window

Avatar of JMMLAN

ASKER

Ok I added additional cases and for testing sake I am a member of AOD Users and Domain Users but I only map the drive for AOD Users.  Here is what I used:
On Error Resume Next

Set objSysInfo = CreateObject("ADSystemInfo")
Set objNetwork = CreateObject("Wscript.Network")

strUserPath = "LDAP://" & objSysInfo.UserName
Set objUser = GetObject(strUserPath)

For Each strGroup in objUser.MemberOf
    strGroupPath = "LDAP://" & strGroup
    Set objGroup = GetObject(strGroupPath)
    strGroupName = objGroup.CN

    Select Case strGroupName
      Case "Abra Users"
            objNetwork.MapNetworkDrive "H:", "\\QPRCDATA01\Abra Suite"        

      Case "AOD Users"
            objNetwork.MapNetworkDrive "M:", "\\QPRCAOD01\AOD"
      
      Case "Resource Users"
            objNetwork.MapNetworkDrive "R:", "\\QPRCDATA01\Resource"      

      Case "Domain Users"
            objNetwork.MapNetworkDrive "S:", "\\QPRCDATA01\Folders Shared Across Departments"

      Case "Domain Users"
            objNetwork.MapNetworkDrive "T:", "\\QPRCDC01\DC01Shared"
       
       
    End Select
Next
ASKER CERTIFIED SOLUTION
Avatar of Todd Gerbert
Todd Gerbert
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of JMMLAN

ASKER

Ok I did what you said with the Case which makes perfect sense and I removed the "On Error" piece.  I am now in 3 groups...Abra Users, AOD Users, and Domain Users but I still only map M and I get nothing back in terms of an error message.
Avatar of JMMLAN

ASKER

THANK YOU...It works once I used it logging in