Announcement

Collapse
No announcement yet.

strange disapearing drive letters

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

  • strange disapearing drive letters

    Hail

    i set a simple script to map network drives according to groups

    Edit by Mod: changed [quote]-tag for [code]-tags 'round the code
    Code:
    '==========================================================================
    '
    ' VBScript Source File -- Created with SAPIEN Technologies PrimalScript 2007
    '
    ' NAME: Ardan Map network Drives according to group membership
    '
    ' AUTHOR: Piratinskiy Haim , ardan
    ' DATE : 10/7/2008
    '
    ' COMMENT: 
    '
    '==========================================================================
    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 "it"
        objNetwork.MapNetworkDrive "X:", "\\act-fs\it"
     
        Case "Amx_sales" 
        objNetwork.MapNetworkDrive "O:", "\\act-fs\amx"
    
        Case "Ardan_hulon_all" 
        objNetwork.MapNetworkDrive "W:", "\\act-fs\public"
    
        Case "Benarit_Users" 
        objNetwork.MapNetworkDrive "K:", "\\act-fs\benarit"
    
        Case "Bmc_users" 
        objNetwork.MapNetworkDrive "M:", "\\act-fs\bmc_proj"
    
        Case "Plc_Users" 
        objNetwork.MapNetworkDrive "L:", "\\act-fs\Plc_Proj"
    
        Case "Plc_Sales" 
        objNetwork.MapNetworkDrive "P:", "\\act-fs\Plc_Sales"
    
        Case "Priority_map" 
        objNetwork.MapNetworkDrive "Z:", "\\act-fs\priority"
    
        Case "Sales_Users" 
        objNetwork.MapNetworkDrive "V:", "\\act-fs\sales"
    
        Case "Security_Users" 
        objNetwork.MapNetworkDrive "T:", "\\act-fs\Security_Proj"
    
        Case "Service_Users" 
        objNetwork.MapNetworkDrive "R:", "\\act-fs\Service_Proj"
    
      End Select
    Next
    
    WScript.Quit
    some how, some of the letters (like M and O) are not appearing from some wierd reason

    any ideas would be appriciated
    Last edited by Rems; 13th October 2008, 10:47.

  • #2
    Re: strange disapearing drive letters

    I suppose the obvious questions are do they exist as shares and does the user have the correct permissions to access them?
    cheers
    Andy

    Please read this before you post:


    Quis custodiet ipsos custodes?

    Comment


    • #3
      Re: strange disapearing drive letters

      First of all, remove the "On Error Resume Next" statement from the top of the script. Then run the script manualy and read error messages.

      Note 1, "Select Case" statements are case sensitive!
      Note 2, the CN name is not the same as the accountname of the group.

      You could add a routine to the script to check if the drive letter might be already in use, mapped i.e. to an usb-drive, cardreader or other unc-path.

      btw. The method you use in the script looping through the values of the MemberOf attribute of the userobject is not a recommended one. The memberOf attribute data type is depending in the count of groups in it, it may contain no Distinguished Names (note, the "Primary Group" is never in this attribute) or may have one Distinguished Name then data type is "String", or more than one then the attribute data type is "Variant()". (from: http://www.rlmueller.net/MemberOf.htm ). So when there is no or only one group in the MemberOf attribute, the For-each loop can not process and will raise an error. Also, your solution cannot find a group when someone set it as the primary group for the user, and it cannot deal with nested groups. There are other ways, and I think more efficient ways, you can use to check for group membership: http://www.rlmueller.net/freecode1.htm



      \Rems


      sample
      Code:
      '# Logon script
      '# to map several network drives according to user's groups
      '# http://forums.petri.com/showthread.php?t=28729
      '#
      '# note, 
      '# use the sAMAccountName of the groupobject as the groupname to compare.
      
      
      Option Explicit
      
      With CreateObject("ADSystemInfo")
        Dim objrootDSE, objUser
        Set objrootDSE = GetObject("LDAP://" & .DomainDNSName & "/rootDSE")
        Set objUser = GetObject("LDAP://" & .UserName)
        '# Create a cache of the user attributes most used in the script
          objUser.GetInfoEx Array("sAMAccountName","tokenGroups"), 0
      End With
      
      '# Declare variables
      DIM objGroupList ' Global Scope
      Dim strUser, arrGrps
      Dim sKey, ln, strGroup
      Dim strDrive, strUNC
      
      
      '# Populate/expand the vbDictionary "GroupList" 
      '# with all groups the objUser is a member off.
      strUser = objUser.sAMAccountName
      CreateGroupList strUser, objUser.Get("tokenGroups")
      
      '( it is possible to add also the COMPUTER's groups to the same list )
      
      '# Loop through the GroupList, filter the USER's groups.
      For Each sKey in objGroupList.Keys
         ln = len(strUser)+1
         If Left(sKey, ln) = strUser & "\" Then
            strGroup = Mid(sKey, ln+1) ' belongs to the user
            Select Case LCase(strGroup)
            '# Compare the sAMAccountName with each groupname in the list.
      
               Case LCase("it")
               strDrive = "X:"
               strUNC   = "\\act-fs\it"
               MapDrive strDrive, strUNC
      
               Case LCase("Amx_sales")
               strDrive = "O:"
               strUNC   = "\\act-fs\amx"
               MapDrive strDrive, strUNC
      
               Case LCase("Ardan_hulon_all")
               strDrive = "W:"
               strUNC   = "\\act-fs\public"
               MapDrive strDrive, strUNC
      
               Case LCase("Benarit_Users")
               strDrive = "K:"
               strUNC   = "\\act-fs\benarit"
               MapDrive strDrive, strUNC
               
               Case LCase("Bmc_users")
               strDrive = "M:"
               strUNC   = "\\act-fs\bmc_proj"
               MapDrive strDrive, strUNC
      
               Case LCase("Plc_Users")
               strDrive = "L:"
               strUNC   = "\\act-fs\Plc_Proj"
               MapDrive strDrive, strUNC
      
               Case LCase("Plc_Sales")
               strDrive = "P:"
               strUNC   = "\\act-fs\Plc_Sales"
               MapDrive strDrive, strUNC
      
               Case LCase("Priority_map")
               strDrive = "Z:"
               strUNC   = "\\act-fs\priority"
               MapDrive strDrive, strUNC
      
               Case LCase("Sales_Users")
               strDrive = "V:"
               strUNC   = "\\act-fs\sales"
               MapDrive strDrive, strUNC
      
               Case LCase("Security_Users")
               strDrive = "T:"
               strUNC   = "\\act-fs\Security_Proj"
               MapDrive strDrive, strUNC
      
               Case LCase("Service_Users")
               strDrive = "R:"
               strUNC   = "\\act-fs\Service_Proj"
               MapDrive strDrive, strUNC
      
            End Select
        End If
        strDrive = Empty
        strUNC = Empty
      Next
      
      ' \------------/
         Wscript.Quit
      ' /------------\
      
      Sub CreateGroupList (ByVal sAMAccName, ByVal arrbytGroups)
          '# Adapted from http://www.rlmueller.net/freecode1.htm
          '# Subroutine to populate dictionary object with group memberships.
          '# An efficient way to test group membership for multiple
          '# objects (users/computers) using the LDAP provider and the "TokenGroups"
          '# attribute. The routine also reveals membership in nested groups and
          '# the primary group. Will not reveal cross-domain group membership.
          '# objGroupList is the dictionary object, with global scope.
          If (IsEmpty(objGroupList) = True) Then
              Set objGroupList = CreateObject("Scripting.Dictionary")
              objGroupList.CompareMode = vbTextCompare
          ElseIf (objGroupList.Exists(sAMAccName & "\") = True) Then
                exit Sub
          End If
          objGroupList.Add sAMAccName & "\", True  '(just keys, no items)
          Dim arrstrGroupSids(), objGroup, j
          If (TypeName(arrbytGroups) = "Byte()") Then
              ReDim arrstrGroupSids(0)
              arrstrGroupSids(0) = OctetToHexStr(arrbytGroups)
              Set objGroup = GetObject("LDAP://" _
                  & "<SID=" & arrstrGroupSids(0) & ">")
              objGroupList.Add sAMAccName & "\" _
                  & objGroup.sAMAccountName, True
              Set objGroup = Nothing
              Exit Sub
          ElseIf (UBound(arrbytGroups) = -1) Then
              Exit Sub
          End If
          ReDim arrstrGroupSids(UBound(arrbytGroups))
          For j = 0 To UBound(arrbytGroups)
              arrstrGroupSids(j) = OctetToHexStr(arrbytGroups(j))
              Set objGroup = GetObject("LDAP://" _
                  & "<SID=" & arrstrGroupSids(j) & ">")
              objGroupList.Add sAMAccName & "\" _
                  & objGroup.sAMAccountName, True
          Next
          Set objGroup = Nothing
      End Sub
      
      Function OctetToHexStr (ByVal arrbytOctet)
          '# http://www.rlmueller.net/freecode1.htm
          '# subFunction to convert OctetString (byte array) to Hex string.
          Dim k
          OctetToHexStr = ""
          For k = 1 To Lenb(arrbytOctet)
              OctetToHexStr = OctetToHexStr _
                  & Right("0" & Hex(Ascb(Midb(arrbytOctet, k, 1))), 2)
          Next
      End Function
      
      Sub MapDrive(ByVal sltr, ByVal spth)
         '# Function created by Remco Simons [NL] October 2007
         '# uncomment one or both optional lines ('?optional) -
         '# to enable the features decribed in the lines just above it.
         Dim objNetwork, colDrives
         Dim i, appDrive, appUNC, GoMapping
         Set objNetwork = CreateObject("Wscript.Network")
         Set colDrives = objNetwork.EnumNetworkDrives
         GoMapping = True
         sPare = GetSpareLetter
         For i = 0 to colDrives.Count-1 Step 2
                 appDrive = colDrives.Item(i)
                 appUNC = colDrives.Item(i+1)
                 If UCase(appDrive) = UCase(sltr) Then
                    If UCase(appUNC) = UCase(spth) Then
                       GoMapping = False 'Drive is Already correctly mapped.
                    Else
                       '# enForce the drive letter if already in use, to
                       '# re-map to the desired path.
                       objNetwork.RemoveNetworkDrive appDrive, True, True
                       wscript.sleep(50)
                       '# Replace the old drive letter with any drive letter
                       '# first available - to keep access to the old path.
      '?optional       objNetwork.MapNetworkDrive sPare, appUNC, True '(Persistent)
                    End If
                 End If
                 If UCase(appUNC) = UCase(spth) Then
                    If Not UCase(appDrive) = UCase(sltr) Then
                       '# enForce just a single drive letter to each 
                       '# duplicate UNC-path. By disconnecting the previously
                       '# mapped drive letter to the same path (recommended).
      '?optional       objNetwork.RemoveNetworkDrive appDrive, True, True
                       wscript.sleep(50)
                    End If
                 End If
         Next
         If GoMapping = True _
             Then objNetwork.MapNetworkDrive sltr, spth, False '(non-Persistent)
      End Sub
      
      Function GetSpareLetter
         '# Find the first available driveletter
         With CreateObject("Scripting.FileSystemObject")
          For i = Asc("D") To Asc("Z") 
              If Not .DriveExists(Chr(i)) Then 
                 GetSpareLetter = Chr(i) & ":" 
                 Exit For 
              End If 
          Next
         End With
      End Function
      Last edited by Rems; 13th October 2008, 18:39.

      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


      • #4
        Re: strange disapearing drive letters

        Just wrote this one this morning...
        I tested on my server and it's working fine.
        If a drive is already in use on the workstation (before starting the mapping) OR mapped, a popup will show that the drive is already in use (It tells you the mapped path too).
        This is just an error trap but at this point you can modify the script, remove the popup and select another drive letter.....What ever you want to do here is possible

        Bert


        'Bertrand Vogel 10/17/08 (updated 10/18/0

        on error resume next
        dim groupmember (),I, G, e, netdrive, netpath, mapped()
        Set objSysInfo = CreateObject("ADSystemInfo")
        Set objUser = GetObject("LDAP://" & objSysInfo.UserName)
        set oFs = WScript.CreateObject("Scripting.FileSystemObject")
        set oDrives = oFs.Drives
        M=0
        D=0
        G=0

        'Check which drives are in use on the local workstation and write the result to an array [drives()]
        strComputer = "."
        for each oDrive in oDrives
        d=d+1
        ReDim Preserve drives (d)
        drives(d) = oDrive.DriveLetter
        'value e is to check if drive is alerady used as a local drive in sub update()
        e=d +1
        next


        '************************************************* ***************
        '* Get the current user's group membership object and write the result to an array.*
        '************************************************* ***************

        objmemberOf = objUser.GetEx("memberOf")
        For Each objGroup in objmemberOf
        G=G+1
        ReDim Preserve groupmember(G)
        objGroup = Mid(objGroup, 4, 330)
        arrGroup = Split(objGroup, "," )
        groupmember(G)= arrGroup(0)
        Next


        Set objNetwork = CreateObject("WScript.Network")
        for I=1 to G
        select case lcase(groupmember(I))



        '*********************************************
        '*Here we map the drives according to group membership*
        '*********************************************

        case lcase("Yearbook Group")
        netdrive = "Y"
        netpath = "\\Adhs01\yearbook"
        update()


        case lcase("kesi group")
        netdrive = "K"
        netpath = "\\Adhs01\kesi"
        update()


        case lcase("student group")
        netdrive = "s"
        netpath = "\\Adhs01\student shared"
        update()


        case lcase("staff group")
        netdrive = "J"
        netpath = "\\Adhs01\staff shared"
        update()

        netdrive = "K"
        netpath = "\\Adhs01\kesi"
        update()

        netdrive = "S"
        netpath = "\\Adhs01\student shared"
        update()

        netdrive = "F"
        netpath = "\\Adhs01\windsor"
        update()


        case lcase("admin-staff group")
        netdrive = "J"
        netpath = "\\Adhs01\staff shared"
        update()

        netdrive = "I"
        netpath = "\\Adhs01\admin-share"
        update()

        netdrive = "F"
        netpath = "\\Adhs01\windsor"
        update()



        '**************************************
        '*end of mapping according to group membership*
        '**************************************

        end select

        Next



        sub update()

        if InStr(Join(drives, arraylenght),netdrive) <> 0 then
        if InStr(Join(drives, arraylenght),netdrive) < e then
        wscript.echo "Drive " & netdrive & ": already in use by a local drive"
        else
        pos1=InStr(Join(mapped, arraylenght),netdrive &":")
        pos2=InStr(pos1,Join(mapped, arraylenght)," ") -1
        pos= pos2 -pos1
        oldpath= mid (Join(mapped, arraylenght),pos1 + 2,pos)
        wscript.echo "Drive " & netdrive & ": IS ALREADY MAPPED TO " & oldpath

        end if
        else
        objNetwork.MapNetworkDrive netdrive & ":" , netpath
        d=d+1
        ReDim Preserve drives (d)
        drives(d) = netdrive

        M= M + 1
        ReDim Preserve mapped (M)
        mapped(M) = netdrive & ":" & netpath & " "
        ' WScript.Echo Join( mapped, arraylenght)

        end if
        end sub
        Last edited by bertv; 18th October 2008, 13:36.

        Comment

        Working...
        X