Click here to Skip to main content
15,891,567 members
Home / Discussions / C#
   

C#

 
AnswerRe: play video in memory stream Pin
Eddy Vluggen16-Oct-12 2:00
professionalEddy Vluggen16-Oct-12 2:00 
QuestionVS2008 deployment not creating directories Pin
Wolfram Steinke15-Oct-12 15:04
Wolfram Steinke15-Oct-12 15:04 
AnswerRe: VS2008 deployment not creating directories Pin
Dave Kreskowiak15-Oct-12 17:29
mveDave Kreskowiak15-Oct-12 17:29 
QuestionNo overload takes 5 arguments error (C# datagrid) Pin
saiprakash031515-Oct-12 11:13
saiprakash031515-Oct-12 11:13 
AnswerRe: No overload takes 5 arguments error (C# datagrid) Pin
Richard Andrew x6415-Oct-12 11:28
professionalRichard Andrew x6415-Oct-12 11:28 
QuestionRe: No overload takes 5 arguments error (C# datagrid) Pin
n.podbielski15-Oct-12 11:43
n.podbielski15-Oct-12 11:43 
AnswerRe: No overload takes 5 arguments error (C# datagrid) Pin
saiprakash031516-Oct-12 5:52
saiprakash031516-Oct-12 5:52 
QuestionConverting from C to C# Pin
AseelHadlaq15-Oct-12 6:53
AseelHadlaq15-Oct-12 6:53 
AnswerRe: Converting from C to C# Pin
fjdiewornncalwe15-Oct-12 7:06
professionalfjdiewornncalwe15-Oct-12 7:06 
AnswerRe: Converting from C to C# Pin
jschell15-Oct-12 7:40
jschell15-Oct-12 7:40 
GeneralRe: Converting from C to C# Pin
AseelHadlaq15-Oct-12 8:30
AseelHadlaq15-Oct-12 8:30 
GeneralRe: Converting from C to C# Pin
jschell15-Oct-12 12:46
jschell15-Oct-12 12:46 
GeneralRe: Converting from C to C# Pin
AseelHadlaq16-Oct-12 5:38
AseelHadlaq16-Oct-12 5:38 
GeneralRe: Converting from C to C# Pin
jschell16-Oct-12 8:36
jschell16-Oct-12 8:36 
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 

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.