VB Script will not map drives based on group membership

I have a VB Script that will not map a drive, unless the person is a member of at least 3 groups in Active Directory.  For example, by default they are a member of "Domain Users", then I will add them to "Accounting".  The script will not recognize that they are a member of Accounting and map a drive letter until I add them to another group in Active Directory.

Any help would be great!
For Each strGroup in objUser.MemberOf       'Walk through the groups that user is a member of
    strGroupPath = "LDAP://" & strGroup       
    Set objGroup = GetObject(strGroupPath)  'Bind to the group in Active Directory   
    strGroupName = objGroup.CN              'Get the group name
 
    strGroupName = lcase(strGroupName)      'Set group name to all lowercase
   
'-------------------------------------BY GROUP--------------------------------------------------------------------
'-----------------------------------------------------------------------------------------------------------------
   
 Select Case strGroupName
 
    Case "hr"   'If in this group map the drive on next line
          objNetwork.MapNetworkDrive "R:", "\\chdifs01\hr$"
 
    Case "accounting"   'If in this group map the drive on next line
          objNetwork.MapNetworkDrive "Q:", "\\chdifs01\accounting$"
 
    Case "mis"   'If in this group map the drive on next line
          objNetwork.MapNetworkDrive "m:", "\\chdifs01\mis$"
 
 End Select

Open in new window

bbanis2kAsked:
Who is Participating?
 
RobSampsonConnect With a Mentor Commented:
Hi bbanis.  There's a couple of issues at work here.

Firstly, "Domain Users" is a primary group, and is not enumerated by the MemberOf property, so it doesn't count as a group membership in this case.  Secondly, objUser.MemberOf is a String data type if the user is only a member of one other group (so it shows 2 in the GUI).  For this reason, the For Each strGroup In objUser.MemberOf statement does not work, because you need an array.  So, that's why, only when you add a second group (3 in the GUI), objUser.MemberOf becomes an array of group names, so the For Each *does* work.

So, I've changed your script a bit.  I have first checked the TypeName, to determine whether objUser.MemberOf is "Empty", a "String", or an "Array".  Then, we don't need to bind to the group to get the group name.  The format of strGroup comes out as
CN=GroupName,OU=MyOU,DC=domain,DC=com

so we know the first part of that is the group name.  We use the Split function to split by the comma, so that element zero is the group name, then we use the Mid function to return the rest of the string after (and including) character 4.  So that this
            strGroupName = Mid(Split(strGroup, ",")(0), 4)

returns just GroupName.

Then I changed your Select Case statement to a sub, so you can call it from anywhere, and don't have to duplicate it.

I hope that helps.

Regards,

Rob.
If TypeName(objUser.MemberOf) = "Empty" Then
	'WScript.Echo "You are not a member of any groups."
ElseIf TypeName(objUser.MemberOf) = "String" Then
	strGroupName = Mid(Split(objUser.MemberOf, ",")(0), 4)
	CheckGroup strGroupName
Else
	For Each strGroup In objUser.MemberOf       'Walk through the groups that user is a member of
		strGroupName = Mid(Split(strGroup, ",")(0), 4)              'Get the group name
	    CheckGroup strGroupName
	Next
End If
   
'-------------------------------------BY GROUP--------------------------------------------------------------------
'-----------------------------------------------------------------------------------------------------------------
 
Sub CheckGroup(strGroupName)
	Set objNetwork = CreateObject("WScript.Network")
	Select Case LCase(strGroupName)
		Case "hr"   'If in this group map the drive on next line
			objNetwork.MapNetworkDrive "R:", "\\chdifs01\hr$"
 
		Case "accounting"   'If in this group map the drive on next line
			objNetwork.MapNetworkDrive "Q:", "\\chdifs01\accounting$"
 
		Case "mis"   'If in this group map the drive on next line
			objNetwork.MapNetworkDrive "m:", "\\chdifs01\mis$"
	End Select
End Sub

Open in new window

0
 
futurefilesCommented:
why not just use IFMEMBER... in a standard bat file?
http://www.microsoft.com/downloads/details.aspx?FamilyID=07c2f6d7-815e-4fa0-9043-4e4635ccd417&displaylang=en

Example

:Accounts
%logonserver%\netlogon\ifmember Accounts
if not errorlevel 1 goto Users
NET USE * /DELETE /YES
NET USE R: \\fileserver\accounts

:Users
if not errorlevel 1 goto End
NET USE * /DELETE /YES
NET USE S: \\fileserver\users

:End
exit
0
 
bbanis2kAuthor Commented:
We use VB Script as our standard.  The script you are looking at is actually about 30 pages long.
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
piattndCommented:
The only suggestion I can give is to add a wscript.echo strGroupName to see the exact phrase that's being read by the computer and the phrase that will be compared against the Case statements.
0
 
piattndCommented:
One more thing, if it's echoing back exactly what you have, but drives are STILL not mapping, add a wscript.echo statement within each of the cases.  That will prove it's entering the appropriate statement, but it's the actual mapping command that's failing, not the case.
0
 
piattndCommented:
Holy crap.... RobSampson not only are you a genius... but you work weekends too!!!

Help me with my gui problem!  ;-)
0
 
RobSampsonCommented:
Ha ha! Thanks....I have learnt most things the hard way though...plenty of hair pulling when things don't go right....plenty of research, and most of the stuff I know has come from writing things here for others.

Nah, my weekend is over now.....7:45 am here on Monday morning....although, I did look at EE over the weekend....I must be sick! LOL!

OK, I saw your Refresh problem....I think the timer will work, I've used that before....I'll have to wait till I get to work in an hour or so to check that out though...

Rob.
0
 
bbanis2kAuthor Commented:
Thank you, sir!  That is awesome to know.
0
 
RobSampsonCommented:
No problem. Thanks for the grade.  I hope I didn't confuse you too much, and explained that OK....

Regards,

Rob.
0
 
bbanis2kAuthor Commented:
No, it makes complete sense.  I really appreciate your help!
0
 
bbanis2kAuthor Commented:
Sorry, but it doesn't work.

Invalid 'for' loop control variable.

Any help would be great...


0
 
RobSampsonCommented:
Hmmm, that's odd.....can I see the line where you get that error?  The code works perfectly fine for me...

Rob.
0
 
bbanis2kAuthor Commented:
I believe it is this line:

 For Each strGroup In objUser.MemberOf       'Walk through the groups that user is a member of
0
 
RobSampsonCommented:
Hmmm....that's what I thought it would have been, as that's the only line with a For loop.

Try this code out for your own account, and see what messages you get back.  It won't map anything.

Regards,

Rob.
Set objADSysInfo = CreateObject("ADSystemInfo")
Set objUser = GetObject("LDAP://" & objADSysInfo.UserName)
If TypeName(objUser.MemberOf) = "Empty" Then
	WScript.Echo "You are not a member of any groups."
ElseIf TypeName(objUser.MemberOf) = "String" Then
	strGroupName = Mid(Split(objUser.MemberOf, ",")(0), 4)
	MsgBox "Single Group:" & VbCrLf & strGroupName
	'CheckGroup strGroupName
Else
	For Each strGroup In objUser.MemberOf       'Walk through the groups that user is a member of
		strGroupName = Mid(Split(strGroup, ",")(0), 4)              'Get the group name
		MsgBox "Multiple Groups:" & VbCrLf & strGroupName
	    'CheckGroup strGroupName
	Next
End If

Open in new window

0
 
bbanis2kAuthor Commented:
Object Required: 'objUser'
0
 
RobSampsonCommented:
Strange......maybe ADSystemInfo doesn't work for you.....

This doesn't work?

Set objADSysInfo = CreateObject("ADSystemInfo")
MsgBox "LDAP://" & objADSysInfo.UserName

Rob.
0
 
bbanis2kAuthor Commented:
Yes, that works just fine.  It gives my LDAP path...
0
 
bbanis2kAuthor Commented:
Hmmm.  Very strange.  The below code tells me all of the groups I am member of now.



Set objADSysInfo = CreateObject("ADSystemInfo")
Set objUser = GetObject("LDAP://" & objADSysInfo.UserName)
If TypeName(objUser.MemberOf) = "Empty" Then
        WScript.Echo "You are not a member of any groups."
ElseIf TypeName(objUser.MemberOf) = "String" Then
        strGroupName = Mid(Split(objUser.MemberOf, ",")(0), 4)
        MsgBox "Single Group:" & VbCrLf & strGroupName
        'CheckGroup strGroupName
Else
        For Each strGroup In objUser.MemberOf       'Walk through the groups that user is a member of
                strGroupName = Mid(Split(strGroup, ",")(0), 4)              'Get the group name
                MsgBox "Multiple Groups:" & VbCrLf & strGroupName
            'CheckGroup strGroupName
        Next
End If

Open in new window

0
 
RobSampsonCommented:
That's good. So that's the same code that I posted just before.....strange that it didn't work the first time.

Anyway, so we know that the For loop *is* working, so now you should be able to add the CheckGroup sub to the bottom of that code, uncomment the CheckGroup strGroupName lines, and it should map the drives.

Regards,

Rob.
0
 
bbanis2kAuthor Commented:
I think we are good.  Thanks for your time...again!
0
 
RobSampsonCommented:
Ah, great.  Strange problems there.....

Have fun...

Rob.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.