Announcement

Collapse
No announcement yet.

VBS script to query group membership

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

  • VBS script to query group membership

    Hi all....

    Ive been "trying" to create a logon script which quiries AD and assigns drive mappings according to group membership. For some reason the standard "domain users" groups maps everything apart from the Y drive.

    Also even though a user isnt in the group which the script queries to map. It seems to hold onto that path as if I try and manually connect to the unc path I cant as windows says its already in use.

    Any ideas is appreciated - thanks
    Attached Files

  • #2
    Re: VBS script to query group membership

    You need a "slash" between the share name and the user name
    WSHNetwork.MapNetworkDrive "Y:", "\\servername\ReleasedData$" & "\" & WSHNetwork.Username & ""
    Tom Jones
    MCT, MCSE (2000:Security & 2003), MCSA:Security & Messaging, MCDBA, MCDST, MCITP(EA, EMA, SA, EDA, ES, CS), MCTS, MCP, Sec+
    PhD, MSc, FIAP, MIITT
    IT Trainer / Consultant
    Ossian Ltd
    Scotland

    ** Remember to give credit where credit is due and leave reputation points where appropriate **

    Comment


    • #3
      Re: VBS script to query group membership

      Originally posted by Ossian View Post
      You need a "slash" between the share name and the user name
      Additionally,
      - since it is a script running at every logon, consider to create just non-persistent mappings for the user.

      Code:
      WSHNetwork.MapNetworkDrive "Y:", "\\servername\ReleasedData$\" & UserString, False
      \Rems

      EDIT -
      removed the doubled double-quotes from the pathstring (those are used in an other script language).
      Last edited by Rems; 24th February 2011, 19:25.

      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: VBS script to query group membership

        Thanks guys, what I dont really get what you mean about non persistant. The users will always want the drives mapped when they log on??

        And as far as I can see there are no variables, or is the group membership a considerd a variable? Its all very confusing... Still doesnt work though :S

        Comment


        • #5
          Re: VBS script to query group membership

          WHat is the exact error you get?
          Tom Jones
          MCT, MCSE (2000:Security & 2003), MCSA:Security & Messaging, MCDBA, MCDST, MCITP(EA, EMA, SA, EDA, ES, CS), MCTS, MCP, Sec+
          PhD, MSc, FIAP, MIITT
          IT Trainer / Consultant
          Ossian Ltd
          Scotland

          ** Remember to give credit where credit is due and leave reputation points where appropriate **

          Comment


          • #6
            Re: VBS script to query group membership

            Sorry, it does work... I was testing the wrong script. But I am still having problems with the fact that:

            I am on Windows XP if I run that script at logon and I am not a member of Admin I dont map the z drive - which is fine. But if I try and map the drive manually with an account which has permissions I get the error the network folder is currently mapped using a different username and password - which it isnt...

            Any ideas??? Thanks

            Comment


            • #7
              Re: VBS script to query group membership

              Check for any cached credentials
              Tom Jones
              MCT, MCSE (2000:Security & 2003), MCSA:Security & Messaging, MCDBA, MCDST, MCITP(EA, EMA, SA, EDA, ES, CS), MCTS, MCP, Sec+
              PhD, MSc, FIAP, MIITT
              IT Trainer / Consultant
              Ossian Ltd
              Scotland

              ** Remember to give credit where credit is due and leave reputation points where appropriate **

              Comment


              • #8
                Re: VBS script to query group membership

                1) The code
                Code:
                For Each GroupObj In UserObj.Groups
                   Select Case GroupObj.Name
                     <...>
                Next
                is unreliable because you can't possibly know the order the groupnames will apear.

                2) There should be a short pause between disconnecting and connecting a drive, when a drive letter is reconnected to an other path.


                The script below should do what I think you want it to do.
                Code:
                rem Option explicit
                
                Dim objGroupList, WSHNetwork
                Dim strDomain, strUser
                
                Set WSHNetwork = CreateObject("WScript.Network")
                call cleanup
                
                strDomain = WSHNetwork.UserDomain
                strUser = WSHNetwork.UserName
                
                With GetObject _
                   ("WinNT://" & strDomain & "/" & strUser & ", user")
                   LoadGroups .Groups
                End With
                
                Select Case TRUE
                   Case objGroupList.Exists("Management")
                   WSHNetwork.MapNetworkDrive "L:", "\\servername\Calidus Mgt", False
                   WSHNetwork.MapNetworkDrive "Y:", "\\servername\ReleasedData$", False
                   Case Else
                   '# Map Y: for all other users
                   WSHNetwork.MapNetworkDrive "Y:", "\\servername\ReleasedData$\" & strUser, False
                End Select
                
                If objGroupList.Exists("Administration") = True Then
                   WSHNetwork.MapNetworkDrive "Z:", "\\servername\Admin", False
                End If
                
                If objGroupList.Exists("SMT Group") = True Then
                   WSHNetwork.MapNetworkDrive "S:", "\\servername\Calidus SMT", False
                End If
                
                If objGroupList.Exists("Finance") = True Then
                   WSHNetwork.MapNetworkDrive "F:", "\\servername\Finance", False
                End If
                
                If objGroupList.Exists("Electronic and SOftware Engineers") = True Then
                   WSHNetwork.MapNetworkDrive "Q:", "\\servername\CalEngElec", False
                End If
                
                '# Map Drives default for all users
                WSHNetwork.MapNetworkDrive "M:", "\\servername\commonmech$", False
                WSHNetwork.MapNetworkDrive "N:", "\\servername\database$", False
                WSHNetwork.MapNetworkDrive "O:", "\\servername\cadmin$", False
                WSHNetwork.MapNetworkDrive "P:", "\\servername\public$", False
                
                
                Sub LoadGroups (ByVal Groups)
                    ' Richard L. Mueller
                    ' Subroutine to populate dictionary object with group memberships.
                    ' objADObject 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 Groups
                        objGroupList.Add objGroup.name, True
                    Next
                End Sub
                
                Sub Cleanup'--> any persistent mapping from a previous session
                   Dim pause, colDrives, i
                   pause = False
                   On Error Resume Next
                   colDrives = array _
                     ("F:","L:","M:","N:","O:","P:","Q:","S:","Y:","Z:")
                   For i = 0 to UBound(colDrives)
                     WSHNetwork.RemoveNetworkDrive colDrives(i), True, True
                     If err.Number = 0 Then pause = True Else err.Clear
                   Next
                   If Cbool(pause) Then wsh.sleep 750
                   On Error Goto 0
                End Sub



                \Rems
                Last edited by Rems; 24th February 2011, 19: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

                Working...
                X