October 17th, 2012, 06:25 PM
Join Date: Jun 2012
Time spent in forums: 3 h 56 m 5 sec
Reputation Power: 2
Extracting Email Addresses from a Email Distribution List
I have the code below which works fine for extracting the email addresses from a distribution group.
The issue is when it tries to recursively get the email and finds a list which has no users or is deactivated it fails there with error :
Active Directory : The directory property cannot be found in the cache.
I want to make sure that it continues after this as well.
Any help pls
strDistListName = "MyDistributionList"
' Get Domain name from RootDSE object.
Set objRootDSE = GetObject("LDAP://RootDSE")
strDomain = objRootDSE.Get("DefaultNamingContext")
'WScript.Echo "Connecting to: " & strDomain
Set ADOconn = CreateObject("ADODB.Connection")
ADOconn.Provider = "ADSDSOObject"
ADOconn.Open "ADs Provider"
bstrADOQueryString = "<LDAP://" & strDomain & ">;(cn=" & strDistListName & ");*;subtree"
Set objRS = ADOconn.Execute(bstrADOQueryString)
If objRS.EOF Then
WScript.Echo "List not found."
Set objObject = GetObject(objRS.Fields(0))
recurse_list objRS.Fields(0), 0
Set objRS = Nothing
Set ADOconn = Nothing
Set objObject = Nothing
Private Function recurse_list(strPath, intLevel)
Set objObject = GetObject(strPath)
objMembers = objObject.GetEx("member")
For Each member In objMembers
Set objMember = GetObject("LDAP://" & member)
If objMember.Class = "group" Then
WScript.Echo Space(intLevel * 3) & "GROUP: " & objMember.cn
recurse_list objMember.ADsPath, intLevel + 1
If Instr(objMember.distinguishedName, "OU=Disabled Accounts") = 0 Then
WScript.Echo Space(intLevel * 3) & objMember.cn
Set objLocMember = Nothing