Re: Empty group
- From: "Richard Mueller [MVP]" <rlmueller-NOSPAM@xxxxxxxxxxxxxxxxxxxx>
- Date: Tue, 31 May 2005 20:32:13 -0500
Mercilon wrote:
> I have a script that I want to run only on AD groups that have members.
How
> do I go about detecting if a group has any members or not?
Hi,
There is no attribute indicating the number of members of a group. For any
group, you could use the Members method of the group object to count the
number of members. Or, the LDAP query below returns groups that have at
least one member:
(&(objectCategory=group)(member=*))
However, this ignores "primary" group membership. The member attribute of
the group object ignores objects that are members because the group is the
"primary" group of the object. The following VBScript outputs the
Distinguished Names of all groups in the domain that have at least one
member:
=================
Option Explicit
Dim objRootDSE, strDNSDomain, objCommand, objConnection
Dim strBase, strFilter, strAttributes, strQuery, objRecordSet
Dim strGroupDN
' Determine DNS domain name.
Set objRootDSE = GetObject("LDAP://RootDSE")
strDNSDomain = objRootDSE.Get("defaultNamingContext")
' Use ADO to search Active Directory.
Set objCommand = CreateObject("ADODB.Command")
Set objConnection = CreateObject("ADODB.Connection")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
objCommand.ActiveConnection = objConnection
strBase = "<LDAP://" & strDNSDomain & ">"
strFilter = "(&(objectCategory=group)(member=*))"
strAttributes = "distinguishedName"
strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
objCommand.CommandText = strQuery
objCommand.Properties("Page Size") = 100
objCommand.Properties("Timeout") = 30
objCommand.Properties("Cache Results") = False
Set objRecordSet = objCommand.Execute
Do Until objRecordSet.EOF
strGroupDN = objRecordSet.Fields("distinguishedName")
Wscript.Echo "Group with members: " & strGroupDN
objRecordSet.MoveNext
Loop
objRecordSet.Close
=================
In the final loop, you could bind to each group with the Distinguished Name.
By default, the "primary" group of user objects is the "Domain Users" group.
In most cases, the above script will not return the group "Domain Users",
because the member attribute is empty - all members have this group
designated as their "primary". Similarily, the group "Domain Computers"
appears to be empty in many cases, because by default computer objects have
this group designated as "primary".
--
Richard
Microsoft MVP Scripting and ADSI
Hilltop Lab web site - http://www.rlmueller.net
--
.
- Follow-Ups:
- Re: Empty group
- From: Mercilon
- Re: Empty group
- References:
- Empty group
- From: Mercilon
- Empty group
- Prev by Date: Re: Log On Script
- Next by Date: Powerful login script??
- Previous by thread: Empty group
- Next by thread: Re: Empty group
- Index(es):
Relevant Pages
|