Is there a way to use directory searcher and get users distribution list that they are members of?
My task is to query ad and get all new accounts (less then 24 hours) and report on users and their distribution lists only. This is so we can make sure they are correctly added and nothing is missed.
I got as far and getting users and their memberof property but that returns everything. Can I filter it to distribution lists only?
All examples online seems to be gettings members of a distrubution list. That isnt what I need. I need to get all of a users distrubition list.
distribution listDirectorySearcherActiveDirectoryFindall
The thread shows how to find users are in a perticular distribution list. That isnt what I am trying to get. I need to know all distribution list that a perticular users belongs to. My query is on the user not on the list.
distribution listDirectorySearcherActiveDirectoryFindall
In the first post he is talking about memberOf - this is what you would probably need. Use DirectorySearcher to find required user and then enumerate his "memberOf"-properties.
Example (not tested)
DirectorySearcher search = new DirectorySearcher(...);
search.Filter = "(SAMAccountName=" + username + ")";
search.PropertiesToLoad.Add("memberOf");
SearchResult result = search.FindOne();
foreach (var g in result.Properties["memberOf"])
{
...
}
MemberOf contains a DN of a group. If your lists located in specific OU (e.g. OU=Distribution Groups)
So I am trying to get my result and generate an email. I want to do 2 columns. First is the user and second is the distribution lists.
From my search searchObject how am I finding the user and then the collection of memberOf for the distribution list that is in the containers "Internal ONLY Distribution Lists" and "Internal-External Distribution Lists"? Any help would be appreciated. The
code below isnt working. I admit I am a bit confused whats giving me a collection and whats giving me an single object below.
SearchResultCollection = searcher.FindAll()
For Each searchResult In searchResultCollection
searchObject = searchResult.getDirectoryEntry()
sMsg.Append("<tr>")
sMsg.Append("<td><font face=""Calibri"">" + searchObject.Properties("distinguishedName").ToString + "</font></td>")
Dim memberOfProperties As PropertyValueCollection = searchObject.Properties("memberOf")
For Each memberofProperty In memberOfProperties
If memberofProperty.ToString.IndexOf("Internal ONLY Distribution Lists") Or memberofProperty.ToString.IndexOf("Internal-External Distribution Lists") Then
sMsg.Append("<td><font face=""Calibri"">" + memberofProperty.ToString + "</font></td>")
End If
Next
sMsg.Append("</tr>")
Next
distribution listDirectorySearcherActiveDirectoryFindall
One of the problems I see is that you do not properly set IF..THEN
The indexof method returns -1, or position which is zero-based.
If you have a group named "Internal ONLY Distribution Lists" and you do IndexOf("Internal ONLY Distribution Lists") then result is 0 - which is FALSE, so when you set
IF FALSE THEN
you will never get the result you want.
Must be
If memberofProperty.ToString.IndexOf("Internal ONLY Distribution Lists") = 0 _
Or memberofProperty.ToString.IndexOf("Internal-External Distribution Lists") = 0 Then
Second thing is that your code will produce a number of columns different than 2.
Must be
sMsg.Append("<td>")
For Each memberofProperty In memberOfProperties
If memberofProperty.ToString.IndexOf("Internal ONLY Distribution Lists") = 0 _
Or memberofProperty.ToString.IndexOf("Internal-External Distribution Lists")=0 Then
sMsg.Append("<font face=""Calibri"">" + memberofProperty.ToString + "</font>")
End If
Next
sMsg.Append("</td>")
Hope this helps.
distribution listDirectorySearcherActiveDirectoryFindall
Member
9 Points
31 Posts
Get Users Distribution lists
Jul 02, 2014 10:57 AM|tashfin.wahid|LINK
Is there a way to use directory searcher and get users distribution list that they are members of?
My task is to query ad and get all new accounts (less then 24 hours) and report on users and their distribution lists only. This is so we can make sure they are correctly added and nothing is missed.
I got as far and getting users and their memberof property but that returns everything. Can I filter it to distribution lists only?
All examples online seems to be gettings members of a distrubution list. That isnt what I need. I need to get all of a users distrubition list.
distribution list DirectorySearcher ActiveDirectory Findall
All-Star
35149 Points
9075 Posts
Re: Get Users Distribution lists
Jul 02, 2014 11:00 AM|smirnov|LINK
http://forums.asp.net/t/1224607.aspx
distribution list DirectorySearcher ActiveDirectory Findall
Member
9 Points
31 Posts
Re: Get Users Distribution lists
Jul 02, 2014 11:20 AM|tashfin.wahid|LINK
The thread shows how to find users are in a perticular distribution list. That isnt what I am trying to get. I need to know all distribution list that a perticular users belongs to. My query is on the user not on the list.
distribution list DirectorySearcher ActiveDirectory Findall
All-Star
35149 Points
9075 Posts
Re: Get Users Distribution lists
Jul 02, 2014 11:52 AM|smirnov|LINK
In the first post he is talking about memberOf - this is what you would probably need. Use DirectorySearcher to find required user and then enumerate his "memberOf"-properties.
Example (not tested)
MemberOf contains a DN of a group. If your lists located in specific OU (e.g. OU=Distribution Groups)
CN=somelist,OU=Distribution Groups,DC=domain,DC=com
then you could easily identify lists from DN.
distribution list DirectorySearcher ActiveDirectory Findall
Member
9 Points
31 Posts
Re: Get Users Distribution lists
Jul 07, 2014 10:48 AM|tashfin.wahid|LINK
So I am trying to get my result and generate an email. I want to do 2 columns. First is the user and second is the distribution lists.
From my search searchObject how am I finding the user and then the collection of memberOf for the distribution list that is in the containers "Internal ONLY Distribution Lists" and "Internal-External Distribution Lists"? Any help would be appreciated. The code below isnt working. I admit I am a bit confused whats giving me a collection and whats giving me an single object below.
SearchResultCollection = searcher.FindAll()
distribution list DirectorySearcher ActiveDirectory Findall
All-Star
35149 Points
9075 Posts
Re: Get Users Distribution lists
Jul 07, 2014 11:47 AM|smirnov|LINK
One of the problems I see is that you do not properly set IF..THEN
The indexof method returns -1, or position which is zero-based.
If you have a group named "Internal ONLY Distribution Lists" and you do IndexOf("Internal ONLY Distribution Lists") then result is 0 - which is FALSE, so when you set
IF FALSE THEN
you will never get the result you want.
Must be
Second thing is that your code will produce a number of columns different than 2.
Must be
Hope this helps.
distribution list DirectorySearcher ActiveDirectory Findall