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?
New code examples in category C#