Click here to Skip to main content
15,885,649 members
Home / Discussions / C#
   

C#

 
AnswerRe: Converting from C to C# Pin
Dave Doknjas15-Oct-12 9:41
Dave Doknjas15-Oct-12 9:41 
GeneralRe: Converting from C to C# Pin
AseelHadlaq16-Oct-12 5:43
AseelHadlaq16-Oct-12 5:43 
AnswerRe: Converting from C to C# Pin
DaveyM6915-Oct-12 11:30
professionalDaveyM6915-Oct-12 11:30 
GeneralRe: Converting from C to C# Pin
AseelHadlaq16-Oct-12 5:45
AseelHadlaq16-Oct-12 5:45 
AnswerRe: Converting from C to C# Pin
Bernhard Hiller15-Oct-12 22:35
Bernhard Hiller15-Oct-12 22:35 
GeneralRe: Converting from C to C# Pin
AseelHadlaq16-Oct-12 5:49
AseelHadlaq16-Oct-12 5:49 
GeneralRe: Converting from C to C# Pin
Pete O'Hanlon16-Oct-12 6:21
mvePete O'Hanlon16-Oct-12 6:21 
QuestionDirectoryEntry Questions Pin
Erick Kinnee15-Oct-12 4:37
Erick Kinnee15-Oct-12 4:37 
Hello all. I think I'm over using DirectoryEntry but I'm not sure how to do what I want in any other way.Confused | :confused:

What this tool is supposed to do is connect to the machines listed in AD and retrieve the members of the "Administrators" group on the local machine.

C#
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.DirectoryServices;
using System.DirectoryServices.ActiveDirectory;
using System.Data;
using System.IO;
using System.Xml;

namespace FindAdmins
{
    class Program
    {
        static void Main(string[] args)
        {
            //Setup the LDAP connection to the domain your user is part of.
            string connectionPrefix = "LDAP://" + FriendlyDomainToLdapDomain(Environment.UserDomainName);
            DirectoryEntry entry = new DirectoryEntry(connectionPrefix);
            DirectorySearcher mySearcher = new DirectorySearcher(entry);
            //Find only computers
            mySearcher.Filter = "(objectClass=computer)";
            //Performance tweaks
            mySearcher.PageSize = 50000;
            mySearcher.SizeLimit = 100000;
            mySearcher.Asynchronous = true;
            //Only want common name
            mySearcher.PropertiesToLoad.Add("cn");
            //Find everything
            SearchResultCollection results = mySearcher.FindAll();
            Console.WriteLine("Number of machines found: " + results.Count);
            int count = 0;

            //DataTable for results
            DataTable table = new DataTable();
            table.TableName = Environment.UserDomainName;
            table.Columns.Add("Machine");
            table.Columns.Add("Group");
            table.Columns.Add("User");

            //Since we have all machines in the domain we loop over them
            foreach (SearchResult result in results)
            {
                //DirectoryEntry direntry = new DirectoryEntry(result.Path);

                //Tracking progress
                count++;
                Console.Write(count + "\r");
                
                try
                {
                    //Connect directly to the machine.
                    DirectoryEntry remMachine = new DirectoryEntry("WinNT://" + result.Properties["cn"][0].ToString() + ",Computer");
                    
                    //Read the members of "Administrators" local group
                    DirectoryEntry admGroup = remMachine.Children.Find("administrators", "group");
                    object members = admGroup.Invoke("members", null);

                    //Loop over the members and see if they are a group themselves.
                    foreach (object groupMember in (IEnumerable)members)
                    {
                        DirectoryEntry member = new DirectoryEntry(groupMember);
                        //If it's a group, get the members.
                        if (member.SchemaClassName == "Group")
                        {
                            object admMembers = member.Invoke("members", null);
                            foreach (object admMember in (IEnumerable)admMembers)
                            {
                                DirectoryEntry membername = new DirectoryEntry(admMember);
                                DataRow dr = table.NewRow();
                                dr["Machine"] = remMachine.Name.ToString();
                                dr["Group"] = member.Name.ToString();
                                dr["User"] = membername.Name.ToString();
                                table.Rows.Add(dr);
                                membername.Dispose();
                            }
                        }
                        //Just a user, write it down
                        else if (member.SchemaClassName == "User")
                        {
                            DataRow dr = table.NewRow();
                            dr["Machine"] = remMachine.Name.ToString();
                            dr["Group"] = "Administrators";
                            dr["User"] = member.Name.ToString();
                            table.Rows.Add(dr);
                            member.Dispose();
                        }
                    }
                    admGroup.Dispose();
                    //member.Close();
                    remMachine.Dispose();
                    GC.Collect();
                }
                catch (Exception e)
                {
                    //writer.WriteLine("  " + e.Message);
                }
                //direntry.Close();
            }
            //table.WriteXml(@"C:\admins.xml", XmlWriteMode.WriteSchema, true);
            table.WriteXml(@"C:\admins.xml", true);
            //table.WriteXmlSchema(@"C:\admins.xsd", true);
            mySearcher.Dispose();
            entry.Dispose();
        }

        //Find the LDAP domain given the NT domain of the user running the tool
        public static string FriendlyDomainToLdapDomain(string friendlyDomainName)
        {
            string ldapPath = null;
            try
            {
                DirectoryContext objContext = new DirectoryContext(DirectoryContextType.Domain, friendlyDomainName);
                Domain objDomain = Domain.GetDomain(objContext);
                ldapPath = objDomain.Name;
            }
            catch (DirectoryServicesCOMException e)
            {
                ldapPath = e.Message.ToString();
            }
            return ldapPath;
        }
    }
}

QuestionRe: DirectoryEntry Questions Pin
Richard MacCutchan15-Oct-12 4:45
mveRichard MacCutchan15-Oct-12 4:45 
AnswerRe: DirectoryEntry Questions Pin
Erick Kinnee15-Oct-12 4:49
Erick Kinnee15-Oct-12 4:49 
GeneralRe: DirectoryEntry Questions Pin
Richard MacCutchan15-Oct-12 5:04
mveRichard MacCutchan15-Oct-12 5:04 
AnswerRe: DirectoryEntry Questions Pin
Eddy Vluggen15-Oct-12 5:04
professionalEddy Vluggen15-Oct-12 5:04 
QuestionMessageBox error Pin
Saridakis Manolis14-Oct-12 23:36
Saridakis Manolis14-Oct-12 23:36 
AnswerRe: MessageBox error Pin
Sivaraman Dhamodharan14-Oct-12 23:39
Sivaraman Dhamodharan14-Oct-12 23:39 
AnswerRe: MessageBox error Pin
Pete O'Hanlon15-Oct-12 0:21
mvePete O'Hanlon15-Oct-12 0:21 
AnswerRe: MessageBox error Pin
Eddy Vluggen15-Oct-12 0:43
professionalEddy Vluggen15-Oct-12 0:43 
QuestionUnhandledException Pin
Hamid_RT14-Oct-12 3:38
Hamid_RT14-Oct-12 3:38 
AnswerRe: UnhandledException Pin
OriginalGriff14-Oct-12 3:49
mveOriginalGriff14-Oct-12 3:49 
GeneralRe: UnhandledException Pin
Hamid_RT14-Oct-12 4:14
Hamid_RT14-Oct-12 4:14 
GeneralRe: UnhandledException Pin
OriginalGriff14-Oct-12 4:25
mveOriginalGriff14-Oct-12 4:25 
GeneralRe: UnhandledException Pin
Hamid_RT14-Oct-12 5:03
Hamid_RT14-Oct-12 5:03 
GeneralRe: UnhandledException Pin
OriginalGriff14-Oct-12 5:12
mveOriginalGriff14-Oct-12 5:12 
GeneralRe: UnhandledException Pin
Hamid_RT14-Oct-12 5:25
Hamid_RT14-Oct-12 5:25 
GeneralRe: UnhandledException Pin
jschell14-Oct-12 6:59
jschell14-Oct-12 6:59 
AnswerRe: UnhandledException Pin
Eddy Vluggen14-Oct-12 3:50
professionalEddy Vluggen14-Oct-12 3:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.