Click here to Skip to main content
12,406,342 members (67,281 online)
   

C#

 
AnswerRe: Converting from C to C# Pin
Dave Doknjas15-Oct-12 9:41
memberDave Doknjas15-Oct-12 9:41 
GeneralRe: Converting from C to C# Pin
AseelHadlaq16-Oct-12 5:43
memberAseelHadlaq16-Oct-12 5:43 
AnswerRe: Converting from C to C# Pin
DaveyM6915-Oct-12 11:30
mentorDaveyM6915-Oct-12 11:30 
GeneralRe: Converting from C to C# Pin
AseelHadlaq16-Oct-12 5:45
memberAseelHadlaq16-Oct-12 5:45 
AnswerRe: Converting from C to C# Pin
Bernhard Hiller15-Oct-12 22:35
memberBernhard Hiller15-Oct-12 22:35 
GeneralRe: Converting from C to C# Pin
AseelHadlaq16-Oct-12 5:49
memberAseelHadlaq16-Oct-12 5:49 
GeneralRe: Converting from C to C# Pin
Pete O'Hanlon16-Oct-12 6:21
protectorPete O'Hanlon16-Oct-12 6:21 
QuestionDirectoryEntry Questions Pin
Erick Kinnee15-Oct-12 4:37
memberErick 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.

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
mvpRichard MacCutchan15-Oct-12 4:45 
AnswerRe: DirectoryEntry Questions Pin
Erick Kinnee15-Oct-12 4:49
memberErick Kinnee15-Oct-12 4:49 
GeneralRe: DirectoryEntry Questions Pin
Richard MacCutchan15-Oct-12 5:04
mvpRichard MacCutchan15-Oct-12 5:04 
AnswerRe: DirectoryEntry Questions Pin
Eddy Vluggen15-Oct-12 5:04
memberEddy Vluggen15-Oct-12 5:04 
QuestionMessageBox error Pin
Saridakis Manolis14-Oct-12 23:36
memberSaridakis Manolis14-Oct-12 23:36 
AnswerRe: MessageBox error Pin
sirama200414-Oct-12 23:39
membersirama200414-Oct-12 23:39 
AnswerRe: MessageBox error Pin
Pete O'Hanlon15-Oct-12 0:21
protectorPete O'Hanlon15-Oct-12 0:21 
AnswerRe: MessageBox error Pin
Eddy Vluggen15-Oct-12 0:43
memberEddy Vluggen15-Oct-12 0:43 
QuestionUnhandledException Pin
Hamid.14-Oct-12 3:38
memberHamid.14-Oct-12 3:38 
AnswerRe: UnhandledException Pin
OriginalGriff14-Oct-12 3:49
mvpOriginalGriff14-Oct-12 3:49 
GeneralRe: UnhandledException Pin
Hamid.14-Oct-12 4:14
memberHamid.14-Oct-12 4:14 
GeneralRe: UnhandledException Pin
OriginalGriff14-Oct-12 4:25
mvpOriginalGriff14-Oct-12 4:25 
GeneralRe: UnhandledException Pin
Hamid.14-Oct-12 5:03
memberHamid.14-Oct-12 5:03 
GeneralRe: UnhandledException Pin
OriginalGriff14-Oct-12 5:12
mvpOriginalGriff14-Oct-12 5:12 
GeneralRe: UnhandledException Pin
Hamid.14-Oct-12 5:25
memberHamid.14-Oct-12 5:25 
GeneralRe: UnhandledException Pin
jschell14-Oct-12 6:59
memberjschell14-Oct-12 6:59 
AnswerRe: UnhandledException Pin
Eddy Vluggen14-Oct-12 3:50
memberEddy 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.


Advertise | Privacy | Mobile
Web02 | 2.8.160730.1 | Last Updated 30 Jul 2016
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid