use of Ismember in VBScript

When I try to run this basic script I get the following error:

********Script************

Dim objNetwork, objSysInfo, strUserDN, objUser, strUser

Set objNetwork = CreateObject("Wscript.Network")

Set objSysInfo = CreateObject("ADSystemInfo")
strUserDN = objSysInfo.userName
strUser = objNetwork.UserName
Set objUser = GetObject("LDAP://" & strUserDN)

If IsMember("IT") Then
objNetwork.MapNetworkDrive "q:", "\\server\testing"
End If

*******************************

**********ERROR****************
Script:  C:\test.vbs
Line: 10
Char: 1
Type mismatch: 'IsMember'
Code: 800A000D
Source: Microsoft VBScript runtime error

*******************************

If I take away the space on line 10 between "if" and "IsMember" then I don't get the same error but instead get this error:


***********ERROR***************
Script:  C:\test.vbs
Line: 10
Char: 18
Error: Expected end of statement
Code: 800A0401
Source: Microsoft VBScript runtime error

*******************************

What am I doing wrong?
LVL 1
AManouxAsked:
Who is Participating?
 
David LeeConnect With a Mentor Commented:
This is the problem.

    If objGrp.IsMember(DBA) Then

objGroup is the group DBA.  You need to test and see if the user is in that group.  So ...

    If objGroup.IsMember(objUser) or If objGroup.IsMember(objUser.adsPath)

I don't remember the exact syntax and am not in a position to look it up at the moment.  One of those should do it though.
0
 
David LeeCommented:
Hi AManoux,

IsMember is a method of a group object, not a stand alone function.  You'll need to use GetObject to get a group and then use IsMember with that group object.  Something like:

objMyGrp = GetObject("LDAP://SomeGroupObject")
If objMyGrp.IsMember(SomeUserObject)

Here's a link to a Microsoft explanation: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/adsi/adsi/iadsgroup_ismember.asp

Cheers!
0
 
AManouxAuthor Commented:
Ok, I added your advice to my new script, but I think I have the syntax still wrong.
I'm trying to check to see if a user is a member of a security group called "DBA" and then to map a drive accordingly. The error I receive says it can't find the object when it sets the objGrp.

*********************************
'  map netork drive for users

Dim objNet, strUserName, strUserDN
Dim objUser

Set objNet = CreateObject("Wscript.Network")
Set objSysInfo = CreateObject("ADSystemInfo")
strUserName = objNet.UserName
strUserDN = objSysInfo.userName

' Bind to the user objects with the LDAP provider.
Set objUser = GetObject("LDAP://" & strUserDN)
Set objGrp = GetObject("LDAP://CN=DBA,OU=Users,DC=domain,DC=com")

' Map a network drive if the user is a member of the group.
If objGrp.IsMember(DBA) Then
  objNet.MapNetworkDrive "T:", "\\server\testing\"
End if

******************************
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
AManouxAuthor Commented:
Got it working. Thanks BlueDevilFan
0
 
David LeeCommented:
You're welcome.  Glad I could help.
0
 
PEI_ITCommented:
Hey There,
This has been really helpful.  However, when my script is run with the following section, I get a "type missmatch: 'IsMember'" error.  I am totally new to vbscript. BlueDevilFan, could  make any suggestions?


' Bind to the user objects with the LDAP provider.

Set objUser = GetObject("LDAP://" & strUserDN)

Set objGroup = GetObject("LDAP://CN=DevWeb,OU=Security,OU=NPL,DC=STAFF,DC=petc,DC=ac,DC=nz")



' Map a network drive if the user is a member of the group.

If objGroup.IsMember(objUser) Then

  objNetwork.MapNetworkDrive "P:", "\\bnplsrv004\www_shares"

End if
0
 
David LeeCommented:
Greetings, PEI_IT.

Try changing this

    If objGroup.IsMember(objUser) Then

to

    If objGroup.IsMember(objUser.ADsPath) Then
0
 
PaulRKruegerCommented:
An old one, but I do have a clarification that may help people in the future. It seems as though people run across the "IsMember" function and try to use it in their scripts but forget to copy the function definitions that go along with it (usually hiding at the bottom of the source script). Here are the supporting functions for the IsMember code:

Function IsMember(strGroup)
' Function to test for group membership.
' strGroup is the NT name (sAMAccountName) of the group to test.
' objGroupList is a dictionary object, with global scope.
' Returns True if the user or computer is a member of the group.

  If IsEmpty(objGroupList) Then
    Call LoadGroups
  End If
  IsMember = objGroupList.Exists(strGroup)
End Function

Sub LoadGroups
' Subroutine to populate dictionary object with group memberships.
' objADObject is the user or computer object, with global scope.
' objGroupList is a dictionary object, with global scope.

  Dim arrbytGroups, j
  Dim arrstrGroupSids(), objGroup

  Set objGroupList = CreateObject("Scripting.Dictionary")
  objGroupList.CompareMode = vbTextCompare

  objADObject.GetInfoEx Array("tokenGroups"), 0
  arrbytGroups = objADObject.Get("tokenGroups")
  If TypeName(arrbytGroups) = "Byte()" Then
    ReDim arrstrGroupSids(0)
    arrstrGroupSids(0) = OctetToHexStr(arrbytGroups)
    Set objGroup = GetObject("LDAP://<SID=" & arrstrGroupSids(0) _
      & ">")
    objGroupList(objGroup.sAMAccountName) = True
    Set objGroup = Nothing
    Exit Sub
  End If
  If UBound(arrbytGroups) = -1 Then
    Exit Sub
  End If

  ReDim arrstrGroupSids(UBound(arrbytGroups))
  For j = 0 To UBound(arrbytGroups)
    arrstrGroupSids(j) = OctetToHexStr(arrbytGroups(j))
    Set objGroup = GetObject("LDAP://<SID=" & arrstrGroupSids(j) _
      & ">")
    objGroupList(objGroup.sAMAccountName) = True
  Next
  Set objGroup = Nothing

End Sub

Function OctetToHexStr(arrbytOctet)
' Function to convert OctetString (byte array) to Hex string.

  Dim k
  OctetToHexStr = ""
  For k = 1 To Lenb(arrbytOctet)
    OctetToHexStr = OctetToHexStr _
      & Right("0" & Hex(Ascb(Midb(arrbytOctet, k, 1))), 2)
  Next
End Function

Open in new window

0
All Courses

From novice to tech pro — start learning today.