locked
Get Users Distribution lists RRS feed

  • Question

  • User2097072074 posted

    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.

    Wednesday, July 2, 2014 10:57 AM

Answers

  • User1508394307 posted

    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)

    CN=somelist,OU=Distribution Groups,DC=domain,DC=com

    then you could easily identify lists from DN.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 2, 2014 11:52 AM

All replies

  • User1508394307 posted

    http://forums.asp.net/t/1224607.aspx 

    Wednesday, July 2, 2014 11:00 AM
  • User2097072074 posted

    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.

    Wednesday, July 2, 2014 11:20 AM
  • User1508394307 posted

    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)

    CN=somelist,OU=Distribution Groups,DC=domain,DC=com

    then you could easily identify lists from DN.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 2, 2014 11:52 AM
  • User2097072074 posted

    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

    Monday, July 7, 2014 10:48 AM
  • User1508394307 posted

    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.

    Monday, July 7, 2014 11:47 AM