[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

VB Script will not map drives based on group membership

Posted on 2008-11-09
21
Medium Priority
?
559 Views
Last Modified: 2012-05-05
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

0
Comment
Question by:bbanis2k
  • 9
  • 8
  • 3
  • +1
21 Comments
 
LVL 4

Expert Comment

by:futurefiles
ID: 22917063
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
 

Author Comment

by:bbanis2k
ID: 22917069
We use VB Script as our standard.  The script you are looking at is actually about 30 pages long.
0
 
LVL 12

Expert Comment

by:piattnd
ID: 22917119
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 12

Expert Comment

by:piattnd
ID: 22917124
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
 
LVL 65

Accepted Solution

by:
RobSampson earned 2000 total points
ID: 22917560
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
 
LVL 12

Expert Comment

by:piattnd
ID: 22917581
Holy crap.... RobSampson not only are you a genius... but you work weekends too!!!

Help me with my gui problem!  ;-)
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 22917625
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
 

Author Closing Comment

by:bbanis2k
ID: 31514857
Thank you, sir!  That is awesome to know.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 22917905
No problem. Thanks for the grade.  I hope I didn't confuse you too much, and explained that OK....

Regards,

Rob.
0
 

Author Comment

by:bbanis2k
ID: 22918601
No, it makes complete sense.  I really appreciate your help!
0
 

Author Comment

by:bbanis2k
ID: 22919126
Sorry, but it doesn't work.

Invalid 'for' loop control variable.

Any help would be great...


0
 
LVL 65

Expert Comment

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

Rob.
0
 

Author Comment

by:bbanis2k
ID: 22919184
I believe it is this line:

 For Each strGroup In objUser.MemberOf       'Walk through the groups that user is a member of
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 22919203
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
 

Author Comment

by:bbanis2k
ID: 22919243
Object Required: 'objUser'
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 22919262
Strange......maybe ADSystemInfo doesn't work for you.....

This doesn't work?

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

Rob.
0
 

Author Comment

by:bbanis2k
ID: 22919275
Yes, that works just fine.  It gives my LDAP path...
0
 

Author Comment

by:bbanis2k
ID: 22919282
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
 
LVL 65

Expert Comment

by:RobSampson
ID: 22919292
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
 

Author Comment

by:bbanis2k
ID: 22919310
I think we are good.  Thanks for your time...again!
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 22919323
Ah, great.  Strange problems there.....

Have fun...

Rob.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Welcome back!  My apologies for taking so long to write part two of this series; it's been a long time coming!  As I promised in Part 1, this article will focus on how to locate those elusive AD properties that you are searching for.  Why is this us…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Integration Management Part 2
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses
Course of the Month18 days, 6 hours left to enroll

830 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