A couple of months back I posted this script that would detect any empty distribution groups and give you the chance to delete them. Another useful thing to do now and again when your examining distribution lists in your Exchange organization is to look at how many members each group has and also what type of objects are members of certain lists. In Exchange apart from just user mailboxes you may have contacts (both internal and external), nested groups, Query based distribution lists or even public folders that are members of a distribution list. So its possible that some lists may have collected unwanted baggage in the normal course of events.
So what I’ve done is come up with 3 different scripts for counting members based on the type of member of each distribution list and then generating a little html report to show the number of each type of object within each distribution list. The first script just does a basic count of the members of each list the second expands any nested groups so it comes up with the true number of members of a list (e.g. if you send a mail to this list how many mailboxes a mail will be delivered to etc). The third script is a script that looks at all the Query based distribution lists (where the first two look just at groups) and expands each list.
The script is broken up into a multitude of ADSI queries the first query that is done is for the purpose of calculating whether contacts that are enumerated by the script have a external or internal target address. To differentiated between internal and external all the domains that are listed in each of the recipient policies are enumerated and then added to a scripting dictionary. The dictionary can then be easily checked to see if the domain of the target email address for the contact is internal or external.
The next query then enumerates all groups that are mail enabled and goes through each group one at a time and looks though the members collection of each group looking at the email address and type of each member. Separate variables track the number of each of the type of objects and if the type of object is a group or dynamic distribution list then the group is expanded and each member of that group is also checked. To guard against circular references a separate scripting dictionary object is maintained to ensure that each object is checked and recorded only once per iteration. A separate sub exists to handle the expansion of query based distribution lists by reading the filter AD attributes and then doing a separate ADSI query baaed on these values. Finally there is some code that then builds the result into a html table to display to the users. The report is named Groupreport.htm and is stored in c:\temp directory.
I’ve put a downloadable copy of the three scripts here basically the three scripts contained in the download are
gnumbs.vbs Only counts member of the current list doesn’t process nested lists
gnumbsnested.vbs Counts all member of a groups including those in nested groups and Query based DL’s
gnumbsqbdl.vbs Displays total number of members for Querybased Distribution lists
the nested group script looks like
set conn = createobject("ADODB.Connection")
set com = createobject("ADODB.Command")
Set iAdRootDSE = GetObject("LDAP://RootDSE")
strDefaultNamingContext = iAdRootDSE.Get("defaultNamingContext")
Conn.Provider = "ADsDSOObject"
Conn.Open "ADs Provider"
report = "<table border=""1"" width=""100%"">" & vbcrlf
report = report & " <tr>" & vbcrlf
report = report & "<td align=""center"" bgcolor=""#000080""><b><font color=""#FFFFFF"">Group-Name</font></b></td>"
& vbcrlf
report = report & "<td align=""center"" bgcolor=""#000080""><b><font color=""#FFFFFF"">Total
Number of Members</font></b></td>" & vbcrlf
report = report & "<td align=""center"" bgcolor=""#000080""><b><font color=""#FFFFFF"">Mailboxes</font></b></td>"
& vbcrlf
report = report & "<td align=""center"" bgcolor=""#000080""><b><font color=""#FFFFFF"">Contacts-Internal</font></b></td>"
& vbcrlf
report = report & "<td align=""center"" bgcolor=""#000080""><b><font color=""#FFFFFF"">Contacts-External</font></b></td>"
& vbcrlf
report = report & "<td align=""center"" bgcolor=""#000080""><b><font color=""#FFFFFF"">Groups</font></b></td>"
& vbcrlf
report = report & "<td align=""center"" bgcolor=""#000080""><b><font color=""#FFFFFF"">QueryBased
Dl's</font></b></td>" & vbcrlf
report = report & "<td align=""center"" bgcolor=""#000080""><b><font color=""#FFFFFF"">Public
Folders</font></b></td>" & vbcrlf
report = report & "<td align=""center"" bgcolor=""#000080""><b><font color=""#FFFFFF"">InterOrg-Person</font></b></td>"
& vbcrlf
report = report & "</tr>" & vbcrlf
trackCicularref = ""
numcheck = 0
usNumberUsers = 0
cnNumberinContacts = 0
cnNumberexContacts = 0
nsNumberGroups = 0
pfNumberPublicFolders = 0
ioNumberIorgPersons = 0
QbNumberQBDLs = 0
set trackmembers = CreateObject("Scripting.Dictionary")
GALQueryFilter = "(&(mail=*)(objectCategory=group))"
strQuery = "<LDAP://" & strDefaultNamingContext & ">;" & GALQueryFilter & ";distinguishedName,displayname,legacyExchangeDN,homemdb;subtree"
Com.ActiveConnection = Conn
Com.Properties("SearchScope") = 2 ' we want to search everything
Com.Properties("Page Size") = 500 ' and we want our records in lots of 500 (must
be < query limit)
polQuery = "<LDAP://" & iAdRootDSE.Get("configurationNamingContext") & ">;(objectCategory=msExchRecipientPolicy);distinguishedName,gatewayProxy;subtree"
Com.CommandText = polQuery
set sdSMTPDomains = CreateObject("Scripting.Dictionary")
Set adRs = Com.Execute
while not adRs.eof
for each adr in adRs.fields("gatewayproxy").value
if instr(lcase(adr),"smtp:") then
if sdSMTPDomains.exists(LCase(right(adr,(len(adr)-instr(adr,"@"))))) then
else
sdSMTPDomains.Add LCase(right(adr,(len(adr)-instr(adr,"@")))),1
end if
end if
next
adRs.movenext
Wend
Com.CommandText = strQuery
Set Rs = Com.Execute
while not rs.eof
trackmembers.RemoveAll
numcheck = 0
usNumberUsers = 0
cnNumberinContacts = 0
cnNumberexContacts = 0
nsNumberGroups = 0
pfNumberPublicFolders = 0
ioNumberIorgPersons = 0
QbNumberQBDLs = 0
trackmembers.add rs.fields("distinguishedName"),1
enumgroup(getobject("LDAP://" &
replace(rs.fields("distinguishedName"),"/","\/")))
wscript.echo rs.fields("displayname")
wscript.echo usNumberUsers
wscript.echo cnNumberinContacts
wscript.echo cnNumberexContacts
wscript.echo nsNumberGroups
wscript.echo QbNumberQBDLs
wscript.echo pfNumberPublicFolders
wscript.echo ioNumberIorgPersons
report = report & "<tr>" & vbcrlf
report = report & "<td align=""center"">" & rs.fields("displayname") &
" </td>" & vbcrlf
report = report & "<td align=""center"">" & numcheck & " </td>" & vbcrlf
report = report & "<td align=""center"">" & usNumberUsers & " </td>" &
vbcrlf
report = report & "<td align=""center"">" & cnNumberinContacts & " </td>" &
vbcrlf
report = report & "<td align=""center"">" & cnNumberexContacts & " </td>" &
vbcrlf
report = report & "<td align=""center"">" & nsNumberGroups & " </td>" &
vbcrlf
report = report & "<td align=""center"">" & QbNumberQBDLs & " </td>" &
vbcrlf
report = report & "<td align=""center"">" & pfNumberPublicFolders &
" </td>" & vbcrlf
report = report & "<td align=""center"">" & ioNumberIorgPersons & " </td>"
& vbcrlf
report = report & "</tr>" & vbcrlf
rs.movenext
wend
report = report & "</table>" & vbcrlf
Set fso = CreateObject("Scripting.FileSystemObject")
set wfile = fso.opentextfile("c:\temp\Groupreport.htm",2,true)
wfile.write report
wfile.close
set wfile = nothing
set fso = nothing
sub enumGroup(objgroup)
for each member in objgroup.members
if Not trackmembers.exists(member.distinguishedName) Then
trackmembers.add member.distinguishedName,1
wscript.echo member.class & " " & member.displayname
If member.mail <> "" then
Select Case member.Class
Case "user" usNumberUsers = usNumberUsers + 1
Case "contact" wscript.echo
right(member.mail,(len(member.mail)-instr(member.mail,"@")))
If
sdSMTPDomains.exists(LCase(right(member.mail,(len(member.mail)-instr(member.mail,"@")))))
Then
cnNumberinContacts = cnNumberinContacts + 1
Else
cnNumberexContacts = cnNumberexContacts + 1
End if
Case "group" nsNumberGroups = nsNumberGroups + 1
enumgroup(getobject("LDAP://" & replace(member.distinguishedName,"/","\/")))
Case "publicFolder" pfNumberPublicFolders = pfNumberPublicFolders + 1
Case "inetOrgPerson" ioNumberIorgPersons = ioNumberIorgPersons + 1
Case "msExchDynamicDistributionList" QbNumberQBDLs = QbNumberQBDLs + 1
enumQBDL(getobject("LDAP://" & replace(member.distinguishedName,"/","\/")))
End Select
numcheck = numcheck + 1
End if
end if
Next
end Sub
Sub enumQBDL(obdlobject)
if obdlobject.msExchDynamicDLBaseDN = "" then
strQuerydl = "<LDAP://" & strDefaultNamingContext & ">;" &
obdlobject.msExchDynamicDLFilter &
";mail,ObjectClass,distinguishedName,displayname,legacyExchangeDN,homemdb;subtree"
else
strQuerydl = "<LDAP://" & obdlobject.msExchDynamicDLBaseDN & ">;" &
obdlobject.msExchDynamicDLFilter &
";mail,ObjectClass,distinguishedName,displayname,legacyExchangeDN,homemdb;subtree"
end if
set com1 = createobject("ADODB.Command")
Com1.ActiveConnection = Conn
Com1.Properties("SearchScope") = 2 ' we want to search everything
Com1.Properties("Page Size") = 500 ' and we want our records in lots of 500
(must be < query limit)
wscript.echo strQuerydl
Com1.CommandText = strQuerydl
Set qdlRs = Com1.Execute
While Not qdlRS.eof
Set member = getobject("LDAP://" &
replace(qdlRS.fields("distinguishedName"),"/","\/"))
if Not trackmembers.exists(member.distinguishedName) Then
trackmembers.add member.distinguishedName,1
wscript.echo member.class & " " & member.displayname
If member.mail <> "" then
Select Case member.Class
Case "user" usNumberUsers = usNumberUsers + 1
Case "contact" wscript.echo
right(member.mail,(len(member.mail)-instr(member.mail,"@")))
If
sdSMTPDomains.exists(LCase(right(member.mail,(len(member.mail)-instr(member.mail,"@")))))
Then
cnNumberinContacts = cnNumberinContacts + 1
Else
cnNumberexContacts = cnNumberexContacts + 1
End if
Case "group" nsNumberGroups = nsNumberGroups + 1
enumgroup(getobject("LDAP://" & replace(member.distinguishedName,"/","\/")))
Case "publicFolder" pfNumberPublicFolders = pfNumberPublicFolders + 1
Case "inetOrgPerson" ioNumberIorgPersons = ioNumberIorgPersons + 1
Case "msExchDynamicDistributionList" QbNumberQBDLs = QbNumberQBDLs + 1
enumQBDL(getobject("LDAP://" & replace(member.distinguishedName,"/","\/")))
End Select
numcheck = numcheck + 1
End if
end if
qdlRS.movenext
wend
End Sub
So what I’ve done is come up with 3 different scripts for counting members based on the type of member of each distribution list and then generating a little html report to show the number of each type of object within each distribution list. The first script just does a basic count of the members of each list the second expands any nested groups so it comes up with the true number of members of a list (e.g. if you send a mail to this list how many mailboxes a mail will be delivered to etc). The third script is a script that looks at all the Query based distribution lists (where the first two look just at groups) and expands each list.
The script is broken up into a multitude of ADSI queries the first query that is done is for the purpose of calculating whether contacts that are enumerated by the script have a external or internal target address. To differentiated between internal and external all the domains that are listed in each of the recipient policies are enumerated and then added to a scripting dictionary. The dictionary can then be easily checked to see if the domain of the target email address for the contact is internal or external.
The next query then enumerates all groups that are mail enabled and goes through each group one at a time and looks though the members collection of each group looking at the email address and type of each member. Separate variables track the number of each of the type of objects and if the type of object is a group or dynamic distribution list then the group is expanded and each member of that group is also checked. To guard against circular references a separate scripting dictionary object is maintained to ensure that each object is checked and recorded only once per iteration. A separate sub exists to handle the expansion of query based distribution lists by reading the filter AD attributes and then doing a separate ADSI query baaed on these values. Finally there is some code that then builds the result into a html table to display to the users. The report is named Groupreport.htm and is stored in c:\temp directory.
I’ve put a downloadable copy of the three scripts here basically the three scripts contained in the download are
gnumbs.vbs Only counts member of the current list doesn’t process nested lists
gnumbsnested.vbs Counts all member of a groups including those in nested groups and Query based DL’s
gnumbsqbdl.vbs Displays total number of members for Querybased Distribution lists
the nested group script looks like
set conn = createobject("ADODB.Connection")
set com = createobject("ADODB.Command")
Set iAdRootDSE = GetObject("LDAP://RootDSE")
strDefaultNamingContext = iAdRootDSE.Get("defaultNamingContext")
Conn.Provider = "ADsDSOObject"
Conn.Open "ADs Provider"
report = "<table border=""1"" width=""100%"">" & vbcrlf
report = report & " <tr>" & vbcrlf
report = report & "<td align=""center"" bgcolor=""#000080""><b><font color=""#FFFFFF"">Group-Name</font></b></td>"
& vbcrlf
report = report & "<td align=""center"" bgcolor=""#000080""><b><font color=""#FFFFFF"">Total
Number of Members</font></b></td>" & vbcrlf
report = report & "<td align=""center"" bgcolor=""#000080""><b><font color=""#FFFFFF"">Mailboxes</font></b></td>"
& vbcrlf
report = report & "<td align=""center"" bgcolor=""#000080""><b><font color=""#FFFFFF"">Contacts-Internal</font></b></td>"
& vbcrlf
report = report & "<td align=""center"" bgcolor=""#000080""><b><font color=""#FFFFFF"">Contacts-External</font></b></td>"
& vbcrlf
report = report & "<td align=""center"" bgcolor=""#000080""><b><font color=""#FFFFFF"">Groups</font></b></td>"
& vbcrlf
report = report & "<td align=""center"" bgcolor=""#000080""><b><font color=""#FFFFFF"">QueryBased
Dl's</font></b></td>" & vbcrlf
report = report & "<td align=""center"" bgcolor=""#000080""><b><font color=""#FFFFFF"">Public
Folders</font></b></td>" & vbcrlf
report = report & "<td align=""center"" bgcolor=""#000080""><b><font color=""#FFFFFF"">InterOrg-Person</font></b></td>"
& vbcrlf
report = report & "</tr>" & vbcrlf
trackCicularref = ""
numcheck = 0
usNumberUsers = 0
cnNumberinContacts = 0
cnNumberexContacts = 0
nsNumberGroups = 0
pfNumberPublicFolders = 0
ioNumberIorgPersons = 0
QbNumberQBDLs = 0
set trackmembers = CreateObject("Scripting.Dictionary")
GALQueryFilter = "(&(mail=*)(objectCategory=group))"
strQuery = "<LDAP://" & strDefaultNamingContext & ">;" & GALQueryFilter & ";distinguishedName,displayname,legacyExchangeDN,homemdb;subtree"
Com.ActiveConnection = Conn
Com.Properties("SearchScope") = 2 ' we want to search everything
Com.Properties("Page Size") = 500 ' and we want our records in lots of 500 (must
be < query limit)
polQuery = "<LDAP://" & iAdRootDSE.Get("configurationNamingContext") & ">;(objectCategory=msExchRecipientPolicy);distinguishedName,gatewayProxy;subtree"
Com.CommandText = polQuery
set sdSMTPDomains = CreateObject("Scripting.Dictionary")
Set adRs = Com.Execute
while not adRs.eof
for each adr in adRs.fields("gatewayproxy").value
if instr(lcase(adr),"smtp:") then
if sdSMTPDomains.exists(LCase(right(adr,(len(adr)-instr(adr,"@"))))) then
else
sdSMTPDomains.Add LCase(right(adr,(len(adr)-instr(adr,"@")))),1
end if
end if
next
adRs.movenext
Wend
Com.CommandText = strQuery
Set Rs = Com.Execute
while not rs.eof
trackmembers.RemoveAll
numcheck = 0
usNumberUsers = 0
cnNumberinContacts = 0
cnNumberexContacts = 0
nsNumberGroups = 0
pfNumberPublicFolders = 0
ioNumberIorgPersons = 0
QbNumberQBDLs = 0
trackmembers.add rs.fields("distinguishedName"),1
enumgroup(getobject("LDAP://" &
replace(rs.fields("distinguishedName"),"/","\/")))
wscript.echo rs.fields("displayname")
wscript.echo usNumberUsers
wscript.echo cnNumberinContacts
wscript.echo cnNumberexContacts
wscript.echo nsNumberGroups
wscript.echo QbNumberQBDLs
wscript.echo pfNumberPublicFolders
wscript.echo ioNumberIorgPersons
report = report & "<tr>" & vbcrlf
report = report & "<td align=""center"">" & rs.fields("displayname") &
" </td>" & vbcrlf
report = report & "<td align=""center"">" & numcheck & " </td>" & vbcrlf
report = report & "<td align=""center"">" & usNumberUsers & " </td>" &
vbcrlf
report = report & "<td align=""center"">" & cnNumberinContacts & " </td>" &
vbcrlf
report = report & "<td align=""center"">" & cnNumberexContacts & " </td>" &
vbcrlf
report = report & "<td align=""center"">" & nsNumberGroups & " </td>" &
vbcrlf
report = report & "<td align=""center"">" & QbNumberQBDLs & " </td>" &
vbcrlf
report = report & "<td align=""center"">" & pfNumberPublicFolders &
" </td>" & vbcrlf
report = report & "<td align=""center"">" & ioNumberIorgPersons & " </td>"
& vbcrlf
report = report & "</tr>" & vbcrlf
rs.movenext
wend
report = report & "</table>" & vbcrlf
Set fso = CreateObject("Scripting.FileSystemObject")
set wfile = fso.opentextfile("c:\temp\Groupreport.htm",2,true)
wfile.write report
wfile.close
set wfile = nothing
set fso = nothing
sub enumGroup(objgroup)
for each member in objgroup.members
if Not trackmembers.exists(member.distinguishedName) Then
trackmembers.add member.distinguishedName,1
wscript.echo member.class & " " & member.displayname
If member.mail <> "" then
Select Case member.Class
Case "user" usNumberUsers = usNumberUsers + 1
Case "contact" wscript.echo
right(member.mail,(len(member.mail)-instr(member.mail,"@")))
If
sdSMTPDomains.exists(LCase(right(member.mail,(len(member.mail)-instr(member.mail,"@")))))
Then
cnNumberinContacts = cnNumberinContacts + 1
Else
cnNumberexContacts = cnNumberexContacts + 1
End if
Case "group" nsNumberGroups = nsNumberGroups + 1
enumgroup(getobject("LDAP://" & replace(member.distinguishedName,"/","\/")))
Case "publicFolder" pfNumberPublicFolders = pfNumberPublicFolders + 1
Case "inetOrgPerson" ioNumberIorgPersons = ioNumberIorgPersons + 1
Case "msExchDynamicDistributionList" QbNumberQBDLs = QbNumberQBDLs + 1
enumQBDL(getobject("LDAP://" & replace(member.distinguishedName,"/","\/")))
End Select
numcheck = numcheck + 1
End if
end if
Next
end Sub
Sub enumQBDL(obdlobject)
if obdlobject.msExchDynamicDLBaseDN = "" then
strQuerydl = "<LDAP://" & strDefaultNamingContext & ">;" &
obdlobject.msExchDynamicDLFilter &
";mail,ObjectClass,distinguishedName,displayname,legacyExchangeDN,homemdb;subtree"
else
strQuerydl = "<LDAP://" & obdlobject.msExchDynamicDLBaseDN & ">;" &
obdlobject.msExchDynamicDLFilter &
";mail,ObjectClass,distinguishedName,displayname,legacyExchangeDN,homemdb;subtree"
end if
set com1 = createobject("ADODB.Command")
Com1.ActiveConnection = Conn
Com1.Properties("SearchScope") = 2 ' we want to search everything
Com1.Properties("Page Size") = 500 ' and we want our records in lots of 500
(must be < query limit)
wscript.echo strQuerydl
Com1.CommandText = strQuerydl
Set qdlRs = Com1.Execute
While Not qdlRS.eof
Set member = getobject("LDAP://" &
replace(qdlRS.fields("distinguishedName"),"/","\/"))
if Not trackmembers.exists(member.distinguishedName) Then
trackmembers.add member.distinguishedName,1
wscript.echo member.class & " " & member.displayname
If member.mail <> "" then
Select Case member.Class
Case "user" usNumberUsers = usNumberUsers + 1
Case "contact" wscript.echo
right(member.mail,(len(member.mail)-instr(member.mail,"@")))
If
sdSMTPDomains.exists(LCase(right(member.mail,(len(member.mail)-instr(member.mail,"@")))))
Then
cnNumberinContacts = cnNumberinContacts + 1
Else
cnNumberexContacts = cnNumberexContacts + 1
End if
Case "group" nsNumberGroups = nsNumberGroups + 1
enumgroup(getobject("LDAP://" & replace(member.distinguishedName,"/","\/")))
Case "publicFolder" pfNumberPublicFolders = pfNumberPublicFolders + 1
Case "inetOrgPerson" ioNumberIorgPersons = ioNumberIorgPersons + 1
Case "msExchDynamicDistributionList" QbNumberQBDLs = QbNumberQBDLs + 1
enumQBDL(getobject("LDAP://" & replace(member.distinguishedName,"/","\/")))
End Select
numcheck = numcheck + 1
End if
end if
qdlRS.movenext
wend
End Sub