David Gard
David Gard

Reputation: 12087

VBS function always returns False when a certain variable is defined

When running the below VBS function to check if the current user is in a certain Security Group, I get error #500 (Variable is undefined) for the line strGroup = LCase(Join(CurrentUser.MemberOf)).

I have Option Explicit declared in the script, so that is not surprising. However, when I do declare the variable (Dim strGroup), the function stops working and always returns false.

Function is_group_member(group)

    Dim objNetwork
    Dim objUser
    Dim CurrentUser

    ' Set our default return value
    is_group_member = False 

    ' Directory Lookup
    Set objNetwork = CreateObject("WScript.Network")
    Set objUser = CreateObject("ADSystemInfo")
    Set CurrentUser = GetObject("LDAP://" & objUser.UserName)

    strGroup = LCase(Join(CurrentUser.MemberOf))    

    ' Set return value to true if the user is in the selected group 
    If InStr(strGroup, lcase(group)) Then
        is_group_member = True  
    End If

End Function

Upvotes: 0

Views: 850

Answers (1)

Binary Worrier
Binary Worrier

Reputation: 51719

At a guess CurrentUser.MemberOf isn't what you think it is.

You need to debug your running script (or if that's not possible, get some logging in there that writes values to the console or to a log file).

You need check

  1. that CurrentUser is not nothing
  2. that CurrentUser.MemberOf is not nothing
  3. that CurrentUser.MemberOf is an array
  4. that CurrentUser.MemberOf is an array of strings
  5. that CurrentUser.MemberOf contains the group that you are expecting.

Use the TypeName function to determine the type of a variable / member

The VBScript debugger can be found here http://www.microsoft.com/en-us/download/details.aspx?id=22185 to Debug the script, install the debugger, then either start the script by passing //x to cscript (cscript //x MyScript.vbs) or put the stop keyword in your script where you want to start debugging

Hope this helps.

Upvotes: 1

Related Questions