Click here to Skip to main content
16,021,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Everyone i am trying to develop a program that will list will all the user of Active Directory and when i select one user the program should be able to show the computers associated with that user. i.e the Computers that are accessible to that AD user. I have written code to list all user but no idea how to list the computers associated with that user. Here is my code to load AD User into datatable:

C#
DataTable dtUser= new DataTable();
    try
    {            
        DirectoryEntry dom = Domain.GetComputerDomain().GetDirectoryEntry();

        DirectorySearcher dsAllUsers = new DirectorySearcher(dom);
        dsAllUsers.SearchScope = SearchScope.Subtree;
        dsAllUsers.Filter = "(objectCategory=Person)";

        SearchResultCollection result = dsAllUsers.FindAll();
        dtUser.Columns.Add("CustodianName");
        dtUser.Columns.Add("Email");
        dtUser.Columns.Add("Title");
        dtUser.Columns.Add("Dept.");           

        foreach (SearchResult rs in result)
        {
            DataRow newRow = dtUser.NewRow();

            if (rs.GetDirectoryEntry().Properties["samaccountname"].Value != null)
                newRow["CustodianName"] = rs.GetDirectoryEntry().Properties["samaccountname"].Value.ToString();

            if (rs.GetDirectoryEntry().Properties["mail"].Value != null)
                newRow["Email"] = rs.GetDirectoryEntry().Properties["mail"].Value.ToString();

            if (rs.GetDirectoryEntry().Properties["title"].Value != null)
                    newRow["Title"] = rs.GetDirectoryEntry().Properties["title"].Value.ToString();

            if (rs.GetDirectoryEntry().Properties["department"].Value != null)
                newRow["Dept."] = rs.GetDirectoryEntry().Properties["department"].Value.ToString();

            dtUser.Rows.Add(newRow);
        }
        return dtUser;
    }
    catch (Exception)
    {
        throw;
    }
Posted
Updated 29-Jun-14 18:55pm
v2
Comments
PIEBALDconsult 30-Jun-14 1:01am    
I don't know, but I suggest you not keep calling rs.GetDirectoryEntry().Properties -- also try the ?? operator.

Also, read this, just in case it matters: http://geekswithblogs.net/mnf/archive/2005/12/20/63581.aspx
Sangit Gurung 30-Jun-14 1:21am    
Thanks Piebal for your comment.Your link was helpful.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900