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

akkisthuban used Ask the Experts™
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.


Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
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.

Most Valuable Expert 2012
Top Expert 2014

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




Thanks guys. I'll give those a shot.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial