none
VBScript and Instr function

    Question

  • I have script where I read in a text file then query a group and look for match of the samAccountName in the file. If it matches I then send an e-mail.  My question is if when I use the Instr function to look for the SamAccountName it will match on either JSmith or JSmith3 etc., Is there anyway to get it to match on the exact name if the one I am If looking for is JSmith and not JSmith3? Code is below:

    Set objGroup = GetObject _  
    ("LDAP://CN=Test,OU=Users and Groups,DC=xxx,DC=xxx")
    objGroup.GetInfo  
    arrMembers = objGroup.GetEx("member")
    For Each strUser in arrMembers
    Set objUser = GetObject("LDAP://" & strUser)
    If  InStr(strContents, objUser.SAMAccountName) Then 
       DoSomething

    End If

    Thanks,
    Matt

    Monday, November 15, 2010 3:56 PM

Answers

  • My recommendation is that if you want an exact match, then do a direct comparison, not an Instr test ...

      If  strContents = objUser.SAMAccountName Then

    This is case sensitive.  To make it case insensitive, try this ...

      If  lcase(strContents) = lcase(objUser.SAMAccountName) Then


    Tom Lavedas
    Monday, November 15, 2010 4:09 PM
    Moderator
  • Tom,

    That is a great suggestion never thought about converting the string to all lowercase and then choosing a match. I appreciate the help.

    Matt

    • Marked as answer by EvilDupa Monday, November 15, 2010 4:30 PM
    Monday, November 15, 2010 4:22 PM

All replies

  • My recommendation is that if you want an exact match, then do a direct comparison, not an Instr test ...

      If  strContents = objUser.SAMAccountName Then

    This is case sensitive.  To make it case insensitive, try this ...

      If  lcase(strContents) = lcase(objUser.SAMAccountName) Then


    Tom Lavedas
    Monday, November 15, 2010 4:09 PM
    Moderator
  • Tom,

    That is a great suggestion never thought about converting the string to all lowercase and then choosing a match. I appreciate the help.

    Matt

    • Marked as answer by EvilDupa Monday, November 15, 2010 4:30 PM
    Monday, November 15, 2010 4:22 PM
  • Tom answered your question. I would only add that the GetEx method will raise an error if the group has no members. I would suggest you trap the possible error. For example:

    Set objGroup = GetObject _  
    ("LDAP://CN=Test,OU=Users and Groups,DC=xxx,DC=xxx") 
    On Error Resume Next
    arrMembers = objGroup.GetEx("member") 
    If (Err.Number = 0) Then
      On Error GoTo 0
      For Each strUser in arrMembers
        Set objUser = GetObject("LDAP://" & strUser)
        If LCase(strContents) = LCase(objUser.SAMAccountName) Then 
          ' DoSomething
        End If
      Next
    End If
    On Error GoTo 0

     

    And, since you bind to each member object anyway, you might as well use the Members method of the group object to retrieve a collection of member objects (which never raises an error). For example:

    Set objGroup = GetObject _  
    ("LDAP://CN=Test,OU=Users and Groups,DC=xxx,DC=xxx") 
    For Each objUser In objGroup.Members
      If LCase(strContents) = LCase(objUser.SAMAccountName) Then 
        ' DoSomething
      End If
    Next

     

     Richard Mueller


    MVP ADSI
    Monday, November 15, 2010 4:31 PM
    Moderator