Need a VBscript that maps network drives depending on user OU memership

Posted on 2007-08-09
Last Modified: 2012-06-27
Hi there.  We are running AD 2000 with XP user pc's. I am in need of a vbscript that maps network drives depending on user OU membership. For example: map drive "R" as \\server\abc is the user is part of the customer support OU. I know nothing about scripting so any help would be greatly appreciated.


Question by:akkisthuban
    LVL 1

    Accepted Solution

    Hi Akkis

    Here is a sample script of how I usually do this, you can paste this into a textfile (with .vbs extension of course):

    On Error Resume Next

    Set objNet = CreateObject("WScript.Network")
    Set objShell = WScript.CreateObject("WScript.Shell")

    '==== Map network drives for GroupName1

    If IsMember("GroupName1") <> 0 Then
    objNet.RemoveNetworkDrive "X:",1
    objNet.MapNetworkDrive "X:", "\\servername1\sharename",True
    End If

    '==== Map network drives for GroupName2

    'If IsMember("GroupName2") <> 0 Then
    'objNet.RemoveNetworkDrive "Y:",1
    'objNet.MapNetworkDrive "Y:", "\\servername1\sharename2",True
    'End If

    '==== Check Group Function

    Private Function IsMember(groupName)

      Set netObj = CreateObject("WScript.Network")
      domain = netObj.UserDomain
      user = netObj.UserName
      flgIsMember = false
      Set userObj = GetObject("WinNT://" & domain & "/" & user & ",user")
      For Each grp In userObj.Groups
        If grp.Name = groupName Then
          flgIsMember = true
          Exit For
        End If
      IsMember = flgIsMember
      Set userObj = Nothing

    End Function

    All you have to do is rename GroupName1/GroupName2 with the groups you want.  Along with the \\servername1\sharename.
    Hope this helps.

    LVL 65

    Expert Comment

    Here is a version that checks the parent OU (the uppermost OU) and maps a drive accordingly:
    Set objSysInfo = CreateObject("ADSystemInfo")
    strName = objSysInfo.UserName

    arrUserName = Split(strName, ",")
    arrOU = Split(arrUserName(1), "=")
    strUserOU = arrOU(1)

    Set objNetwork = CreateObject("WScript.Network")

    Select Case strUserOU
        Case "Customer Support"
            objNetwork.MapNetworkDrive "Y:", "\\servername1\sharename1",True
        Case "Finance"
            objNetwork.MapNetworkDrive "Y:", "\\servername1\sharename2",True
        Case "Human Resources"
            objNetwork.MapNetworkDrive "Y:", "\\servername2\sharename3",True
        Case Else
            ' do not map any drives
    End Select



    Author Comment

    Thanks guys. I'll give those a shot.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Join & Write a Comment

    Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
    If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
    An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
    This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…

    746 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

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now