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

Posted on 2007-08-09
Medium Priority
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

Accepted Solution

kristjanpk earned 2000 total points
ID: 19666717
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

ID: 19667015
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

ID: 19667117
Thanks guys. I'll give those a shot.

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
Let's recap what we learned from yesterday's Skyport Systems webinar.
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

850 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