Link to home
Start Free TrialLog in
Avatar of dmcconnell68
dmcconnell68Flag for United States of America

asked on

VBscript to list all mail enabled active directory groups with their members

I am looking for a vbscript that will list all active directory groups that are mail enabled with the members of each group.  You would think this would be easy to find, but I've been googling for a couple days and havent found exactly what I need.

Thanks
Avatar of RobSampson
RobSampson
Flag of Australia image

Hi there, as a start, see if this provides you with all of the mail enabled group names. It just checks whether an email address has been set or not.

Regards,

Rob.
Const ADS_SCOPE_SUBTREE = 2
 
Dim objRoot, strDNSDomain, objConn, objCommand, objRS
Set objRoot = GetObject("LDAP://RootDSE")
strDNSDomain = "LDAP://" & objRoot.Get("defaultNamingContext")
Set objConn = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConn.Provider = "ADsDSOObject"
objConn.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConn
 
cmd.CommandText = "SELECT ADsPath,cn FROM '" & strDNSDomain & "' WHERE mail='*' AND objectClass='group'"
cmd.Properties("Page Size") = 1000
cmd.Properties("Timeout") = 300
cmd.Properties("Searchscope") = ADS_SCOPE_SUBTREE
 
Set objRS = cmd.Execute
strResults = "Mail enabled groups:"
While Not objRS.EOF
	strResults = strResults & VbCrLf & objRS.Fields("cn").Value
	objRS.MoveNext
Loop
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objOutput = objFSO.CreateTextFile("MailEnabledGroups.txt", True)
objOutput.Write strResults
objOutput.Close
Set objOutput = Nothing
Set objFSO = Nothing
Set objRS = Nothing
Set cmd = Nothing
Set cn = Nothing
Set objDomain = Nothing
Set objRoot = Nothing
MsgBox "Done. Please see MailEnabledGroups.txt"

Open in new window

Avatar of dmcconnell68

ASKER

Hi, thanks for the script, but I'm getting the following errors:

(22, 1) Microsoft VBScript compilation error: 'loop' without 'do'
I changed line 22 from Loop to Wend, then I'm getting the following:

(12, 1) Microsoft VBScript runtime error: Object required: 'cmd'

Thanks for any help.

David
Oh sorry....that's what I get for not testing.....try this.

Regards,

Rob.
Const ADS_SCOPE_SUBTREE = 2
 
Dim objRoot, strDNSDomain, objConn, objCommand, objRS
Set objRoot = GetObject("LDAP://RootDSE")
strDNSDomain = "LDAP://" & objRoot.Get("defaultNamingContext")
Set objConn = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConn.Provider = "ADsDSOObject"
objConn.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConn
 
objCommand.CommandText = "SELECT ADsPath,cn FROM '" & strDNSDomain & "' WHERE mail='*' AND objectClass='group'"
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Timeout") = 300
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
 
Set objRS = objCommand.Execute
strResults = "Mail enabled groups:"
While Not objRS.EOF
	strResults = strResults & VbCrLf & objRS.Fields("cn").Value
	objRS.MoveNext
Wend
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objOutput = objFSO.CreateTextFile("MailEnabledGroups.txt", True)
objOutput.Write strResults
objOutput.Close
Set objOutput = Nothing
Set objFSO = Nothing
Set objRS = Nothing
Set objCommand = Nothing
Set objConn = Nothing
Set objDomain = Nothing
Set objRoot = Nothing
MsgBox "Done. Please see MailEnabledGroups.txt"

Open in new window

Thank you, that works perfectly.  Now, how can I add the display name for each member of each group.  I would like the output to appear something like this:

Human Resources          John Smith, Pete Wright, Sally Jones
Payroll                             Denise Smith, Celia Smith
...
...

Thanks,
David

Do you want the output in CSV or Text format?

Rob.
CSV would be great.  Thank you.
ASKER CERTIFIED SOLUTION
Avatar of RobSampson
RobSampson
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you Rob, this is exactly what I needed.
No problem.  Thanks for the grade.

Regards,

Rob.