Remover usuário de Grupo AD c#

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Data;using System.Collections;using System.DirectoryServices;using System.IO;  namespace ADUsersFromGroup{    class Program    {        static ArrayList _userNames = new ArrayList();        static ArrayList _groupNames = new ArrayList();         static void Main(string[] args)        {            Console.WriteLine("Informe o nome do grupo AD e pressione ENTER: ");            string group = Console.ReadLine();            string ldap = Console.ReadLine();            ArrayList list = GetADGroupUsers(group, ldap);            using (StreamWriter sr = new StreamWriter("c:\\usuarios.txt", false))            {                foreach (var item in list)                    sr.WriteLine("{0}", item);            }            Console.WriteLine("********** FIM **********");        }          static ArrayList GetADGroupUsers(string groupName, string ldap)        {            SearchResultCollection results;            DirectorySearcher search = new DirectorySearcher(ldap);            search.Filter = String.Format("(cn={0})", groupName);            results = search.FindAll();            foreach (SearchResult result in results)            {                ResultPropertyCollection resultPropColl = result.Properties;                foreach (Object memberColl in resultPropColl["member"])                {                    DirectoryEntry gpMemberEntry = new DirectoryEntry("LDAP://" + memberColl);                    System.DirectoryServices.PropertyCollection userProps = gpMemberEntry.Properties;                    object objetoAD = userProps["sAMAccountName"].Value;                    if ((userProps["objectClass"].Value as object[]).Contains<object>("group"))                    {                        // valida se já percorremos este grupo                        if (!_groupNames.Contains(objetoAD))                        {                            _groupNames.Add(objetoAD.ToString());                            Console.WriteLine("Explodindo membros do grupo {0} ...", objetoAD);                            GetADGroupUsers(objetoAD.ToString());                        }                    }                    else                    {                        if (!_userNames.Contains(objetoAD))                        {                            _userNames.Add(objetoAD.ToString());                            Console.WriteLine("      Incluindo {0} ...", objetoAD);                        }                    }                }            }            return _userNames;        }    }}

Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source