Announcement

Collapse
No announcement yet.

Help with Logon Script (Drive Mappings)

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Help with Logon Script (Drive Mappings)

    Hey All,

    We currently use the below logon script to map network drives for our users upon logon. It is intended to map users to there department drive ( J: ) based on group memberships. The script has worked well for us until recently, we are now running into an issue where out of need we have users belonging to multiple user groups.

    I am not the author of the script nor do I know much about scripting, I was hoping someone here could lend me a hand. Basically what I would like to do is add some logic to the script that once a user is identified with a group and a J: drive is mapped it would skip to the end. I guess it would be an "If, Then" type statement. This would prevent it from processing any further groups.

    Any help would be greatly appreciated! Below is the logon script:

    Mod: Added [code] tags around the code
    Code:
    ON ERROR RESUME NEXT
    
    Dim WSHShell, WSHNetwork, objDomain, DomainString, UserString, UserObj, Path
    
    
    Set WSHShell = CreateObject("WScript.Shell")
    Set WSHNetwork = CreateObject("WScript.Network")
    'Automatically grab the user's domain name
    DomainString = Wshnetwork.UserDomain
    'Find the Windows Directory
    WinDir = WshShell.ExpandEnvironmentStrings("%WinDir%")
    
    'Grab the user name
    UserString = WSHNetwork.UserName
    'Bind to the user object to get user name and check for group memberships later
    Set UserObj = GetObject("WinNT://" & DomainString & "/" & UserString)
    
    'Grab the computer name for use in add-on code later
    strComputer = WSHNetwork.ComputerName
    
    
    'Disconnect any drive mappings as needed.
    WSHNetwork.RemoveNetworkDrive "J:", True, True
    WSHNetwork.RemoveNetworkDrive "P:", True, True
    WSHNetwork.RemoveNetworkDrive "V:", True, True
    
    'Disconnect ALL mapped drives
    'Set clDrives = WshNetwork.EnumNetworkDrives
    'For i = 0 to clDrives.Count -1 Step 2
    '    WSHNetwork.RemoveNetworkDrive clDrives.Item(i), True, True
    'Next
    
    'Give the PC time to do the disconnect, wait 300 milliseconds
    wscript.sleep 300
    
    'Map drives needed by all
    'Note the first command uses the user name as a variable to map to a user share.
    'Note alternate mapping code in Add-In section that allows
    'friendly renaming of drive mappings
    
    'WSHNetwork.MapNetworkDrive "H:", "\\server\users\" & UserString,True
    WSHNetwork.MapNetworkDrive "V:", "\\server\Share",True
    
    'Map a shared drive to a resource that requires separate username and password
    'WSHNetwork.MapNetworkDrive "Y:", "\\servername\share", "False", "username", "password"
    
    
    'Now check for group memberships and map appropriate drives
    'Note that this checks Global Groups and not domain local groups.
    For Each GroupObj In UserObj.Groups
    'Force upper case comparison of the group names, otherwise this is case sensitive.
        Select Case UCase(GroupObj.Name)
        'Check for group memberships and take needed action
        'In this example below, ADMIN and WORKERB are groups.
        'Note the use of all upper case letters as mentioned above.
        'Note also that the groups must be Global Groups.
            Case "GG-GROUP1"
                WSHNetwork.MapNetworkDrive "j:", "\\server\share\Departments\Group1",True
        Case "GG-GROUP2"
                WSHNetwork.MapNetworkDrive "j:", "\\server\share\Departments\GROUP2",True
        Case "GG-GROUP3"
                WSHNetwork.MapNetworkDrive "j:", "\\server\share\Departments\GROUP3",True      
    
            'Below is an example of how to set the default printer
            'WSHNetwork.SetDefaultPrinter "\\ServerName\PrinterName"
        End Select
    Next
    
    'Clean Up Memory We Used
    set UserObj = Nothing
    set GroupObj = Nothing
    set WSHNetwork = Nothing
    set DomainString = Nothing
    set WSHSHell = Nothing
    Set WSHPrinters = Nothing
      
    
    'Quit the Script
    wscript.quit
    Last edited by Rems; 12th November 2009, 12:04.
    MCSA/MCSE 2K3,MCITP:ESA,MCTS x 4,VCP x 2

  • #2
    Re: Help with Logon Script (Drive Mappings)

    Here is the script I use. Very similar to yours except no editing of the script at all. You write in the group name, drive letter and share name into a csv file, the script reads the csv and maps the drives accordingly.

    it will do printers as well.....

    http://www.lazynetworkadmin.com/know...-network-drive
    Daniel Frei
    -Windows Operations Server Administrator
    -Exchange Guru
    -Cisco Fanatic
    -SharePoint Hippie
    -Volkswagen Enthusiast

    www.lazynetworkadmin.com

    Comment


    • #3
      Re: Help with Logon Script (Drive Mappings)

      Hey,
      Use this
      "net use <Drive letter>: \\<ip/Hostname>\<Shared folder name>"
      save as bat and map the drives.

      Comment


      • #4
        Re: Help with Logon Script (Drive Mappings)

        Sample of adding: exit For after the mapping was done.
        Code:
        Dim WSHNetwork, DomainString, UserString, UserObj
        
        Set WSHNetwork = CreateObject("WScript.Network")
        
        ' Automatically grab the user's domain name
        DomainString = Wshnetwork.UserDomain
        
        ' Grab the user name
        UserString = WSHNetwork.UserName
        
        ' Bind to the user object with the WinNT provider.
        Set UserObj = GetObject("WinNT://" _
           & DomainString & "/" & UserString & ",user")
        
        For Each GroupObj In UserObj.Groups
           ' Force upper case comparison of the group names, otherwise
           ' this is case sensitive.
           Select Case UCase(GroupObj.Name)
             ' Check for group memberships and take needed action
             ' In this example below, ADMIN and WORKERB are groups.
             ' Note the use of all upper case letters as mentioned above.
             ' Note also that the groups must be Global Groups.
        
             Case "GG-GROUP1"
               WSHNetwork.MapNetworkDrive "j:", _
                   "\\server\share\Departments\Group1",False
               exit For
        
             Case "GG-GROUP2"
               WSHNetwork.MapNetworkDrive "j:", _
                   "\\server\share\Departments\GROUP2",False
               exit For
        
             Case "GG-GROUP3"
               WSHNetwork.MapNetworkDrive "j:", _
                   "\\server\share\Departments\GROUP3",False
               exit For      
        
           End Select
        Next
        EDIT
        Actually, in your scenario 'exit For' will not do the job correctly!!!
        What the sample above does is compare the name of the group in order of appearance retrieved from the user object in AD. But what your script should do is compare your first groupname with all groups stored in the user object, if there is no match then go to your next provided groupname, and so on.

        Therefore, please replace the red colored part from the previous sample with this code:

        snippet
        Code:
        Select Case TRUE
           ' Check for group memberships and take needed action
           ' In this example below, ADMIN and WORKERB are groups.
           ' Note that the groups must be Global Groups.
           '
           ' No need to Force upper case comparison of the group names
           ' this is case insensitive.
        
           Case IsMember("GG-GROUP1")
               WSHNetwork.MapNetworkDrive "j:", _
                  "\\server\share\Departments\Group1",False
        
           Case IsMember("GG-GROUP2")
             WSHNetwork.MapNetworkDrive "j:", _
               "\\server\share\Departments\GROUP2",False
        
           Case IsMember("GG-GROUP3")
             WSHNetwork.MapNetworkDrive "j:", _
               "\\server\share\Departments\GROUP3",False
        
           Case Else
             rem "Not a member"
        
           Function IsMember(strGroup)
             ' Function to test for group membership, 
             ' originally by Richard L. Mueller
             ' @ http://www.rlmueller.net/freecode1.htm see: "Function 1"
             ' This version does NOT reveal "Nested Group" memberships,
             ' but does include membership in the "Primary Group".
        
             ' strGroup is the NT name (sAMAccountName) of the group to test.
             ' objGroupList is a dictionary object.
             ' Returns True if the user is a member of the group.
        
             Dim objGroupList
             If (IsEmpty(objGroupList) = True) _
               Then Call LoadGroups(objGroupList)
        
             IsMember = objGroupList.Exists(strGroup)
           End Function
           Sub LoadGroups(objGroupList)
             ' Subroutine to populate dictionary object with group memberships.
             ' UserObj is the user object, with global scope.
             ' objGroupList is a dictionary object, with global scope.
        
             Dim objGroup
             Set objGroupList = CreateObject("Scripting.Dictionary")
             objGroupList.CompareMode = vbTextCompare
             For Each objGroup In UserObj.Groups
               objGroupList.Add objGroup.name, True
             Next
             Set objGroup = Nothing
           End Sub
        
        End Select



        \Rems
        Last edited by Rems; 19th November 2009, 11:46.

        This posting is provided "AS IS" with no warranties, and confers no rights.

        __________________

        ** Remember to give credit where credit's due **
        and leave Reputation Points for meaningful posts

        Comment

        Working...
        X