Vbs script to get group membership count. Does not get actual count for some groups.

bsharath
bsharath used Ask the Experts™
on
Hi,

Vbs script to get group membership count. Does not get actual count for some groups.
For some groups that are 0 it gets the previously scanned group no's

Can anyone help to fix the issue.

REgards
sharath
On Error Resume Next
 
Const ADS_SCOPE_SUBTREE = 2
 
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand =   CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection
 
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE 
 
domainPath = "DC=lth,Dc=local"
orgUnitPathForGroups = "OU=Distri,OU=Offices"
 
objCommand.CommandText = _
    "SELECT Name FROM 'LDAP://" & orgUnitPathForGroups & "," & domainPath & "' WHERE objectCategory='group'"  
Set objGroupRecordSet = objCommand.Execute
 
objGroupRecordSet.MoveFirst
Do Until objGroupRecordSet.EOF
    
 
    Set objGroup = GetObject _
      ("LDAP://cn=" & objGroupRecordSet.Fields("Name").Value & "," & orgUnitPathForGroups & "," & domainPath)
    objGroup.GetInfo
 
    arrMemberOf = objGroup.GetEx("member")
 
    i = 0
'   WScript.Echo "Members:"
    For Each strMember in arrMemberOf
'        WScript.echo "   " & strMember
        i = i+1
    Next
 
    Wscript.Echo "Group: [" & objGroupRecordSet.Fields("Name").Value & "] Members: " & i
 
    objGroupRecordSet.MoveNext
Loop

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Your problem is this

arrMemberOf = objGroup.GetEx("member")

in line 29, in combination with the leading "on error resume next"....

When a group is empty, the "member" attribute doesn't exist and therefor cannot be read...so this is a runtime error normally, but in your case the script continues, and the "arrMemberOf" will be the same value as the last group!!

Easy solution: Set the "arrMemberOf" to an empty array BEFORE you try to read this:

arrMemberOf = Array()
arrMemberOf = objGroup.GetEx("member")


Philipp

Author

Commented:
Thanks
Can you please make the changes in the whole code

voila
On Error Resume Next
 
Const ADS_SCOPE_SUBTREE = 2
 
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand =   CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection
 
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE 
 
domainPath = "DC=lth,Dc=local"
orgUnitPathForGroups = "OU=Distri,OU=Offices"
 
objCommand.CommandText = _
    "SELECT Name FROM 'LDAP://" & orgUnitPathForGroups & "," & domainPath & "' WHERE objectCategory='group'"  
Set objGroupRecordSet = objCommand.Execute
 
objGroupRecordSet.MoveFirst
Do Until objGroupRecordSet.EOF
    
 
    Set objGroup = GetObject _
      ("LDAP://cn=" & objGroupRecordSet.Fields("Name").Value & "," & orgUnitPathForGroups & "," & domainPath)
    objGroup.GetInfo
 

    arrMemberOf = objGroup.GetEx("member")
    if (Err.Number <> 0) then
         arrMemberOf = Array()
    end if
 
    i = 0
'   WScript.Echo "Members:"
    For Each strMember in arrMemberOf
'        WScript.echo "   " & strMember
        i = i+1
    Next
 
    Wscript.Echo "Group: [" & objGroupRecordSet.Fields("Name").Value & "] Members: " & i
 
    objGroupRecordSet.MoveNext
Loop

Open in new window

Author

Commented:
I get a lot of 0's zeros as membership count. Where as there are members

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial