Need to Map Drives Using Nested Group Memberships via Login Script in Group Policy VB Script


Here's my script UserA is a member of GroupA. Script works if UserA is a direct member of GroupA, but when I nest another group let's call it GroupA.1, the script doesn't work. Can someone tweak the VB code to recognize nested groups?

i.e.,
GroupA group
      GroupA.1 group
            UserA user

Thank you!


Set wshNetwork = WScript.CreateObject("WScript.Network")
      workstation = WshNetwork.computername

wshnetwork.mapnetworkdrive "V:", "\\server1\Folder"
On Error Resume Next
Dim strUserName
Set objSysInfo = CreateObject("ADSystemInfo")
Set objNetwork = CreateObject("Wscript.Network")
      strUserPath = "LDAP://" & objSysInfo.UserName
      strUserName = objNetwork.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 "GroupA"
      objNetwork.MapNetworkDrive "V:", "\\server1\Folder"
End Select
Next
mmoyaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RobSampsonCommented:
Hi,

Nested groups are more difficult, but this should work, provided that you do not have any circular references.  If you have a circular recursive group membership, this will run forever.

Change GROUPA and GROUPB to suit.

Regards,

Rob.
Set objNetwork = CreateObject("WScript.Network")
Set objSysInfo = CreateObject("ADSystemInfo")
'MsgBox objSysInfo.UserName
Set objUser = GetObject("LDAP://" & objSysInfo.UserName)

strGroups = ""

intLevel = 0

GetMemberOfNames objUser, intLevel

If InStr(LCase(";" & Join(Split(Replace(strGroups, ">", ""), VbCrLf), ";") & ";"), LCase(";GROUPA;")) > 0 Then
	objNetwork.MapNetworkDrive "V:", "\\server1\Folder"
ElseIf InStr(LCase(";" & Join(Split(Replace(strGroups, ">", ""), VbCrLf), ";") & ";"), LCase(";GROUPB;")) > 0 Then
	objNetwork.MapNetworkDrive "V:", "\\server2\Folder2"
End If

Sub GetMemberOfNames(objObjectToCheck, intLevel)
	' This function can get caught in a loop if there is a circular
	' group membership.  There is a method of using a Dictionary object
	' here: http://www.rlmueller.net/MemberOf.htm
	' which checks if the group has been used before.
	
	intLevel = intLevel + 1
	' Retrieve ALL of the user groups that a user is a member of
	On Error Resume Next
	objMemberOf = objObjectToCheck.GetEx("MemberOf")
	If Err.Number = 0 Then
		On Error GoTo 0
		For Each objGroup in objMemberOf
			strGroupName = Left(Mid(objGroup, InStr(objGroup, "CN=") + 3),InStr(Mid(objGroup, InStr(objGroup, "CN=") + 3), ",") - 1)
			If strGroups = "" Then
				strGroups = String(intLevel, ">") & strGroupName
			Else
				strGroups = strGroups & VbCrLf & String(intLevel, ">") & strGroupName
			End If
			Set objNextGroup = GetObject("LDAP://" & objGroup)
			GetMemberOfNames objNextGroup, intLevel
		Next
		intLevel = intLevel - 1
	Else
		intLevel = intLevel - 1
		Err.Clear
		On Error GoTo 0
	End If
End Sub

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mmoyaAuthor Commented:
@RobSampson: - Thank you!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.