Tirer de l'information AD dans Excel en utilisant C #

Je suis nouveau sur C # et je cherche à extraire des données d'un Active Directory à l'aide de C # et à le publier dans un file excel. J'ai examiné quelques tutoriels et je suis confus quant à la façon de le faire. Je cherche à get l'ID, le département et le nom de l'user. Comment ferais-je cela en utilisant une fonctionnalité .FindAll ()?

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.DirectoryServices; namespace ADquery { class UserLookUp { static void Main(ssortingng[] args) { Console.WriteLine(resortingeveUsers()); Console.ReadLine(); } public static ssortingng resortingeveUsers() { ssortingng path = ("LDAP://OU=IS,OU=People,DC=Corporate,DC=Amfam,DC=com"); //Init instance of DirectoryEntry DirectoryEntry dEntry = new DirectoryEntry(path); DirectorySearcher dSearcher = new DirectorySearcher(dEntry); //Establish filter (all users)? dSearcher.Filter = "(&(objectCategory=person)(objectClass=user)(cn=*))"; foreach (SearchResult searchResults in dSearcher.FindAll()) { if (searchResults.Properties["CN"][0].ToSsortingng() != null) { //pull info desired ResultPropertyValueCollection valueCollection = searchResults.Properties["manager"]; foreach (Object propertyValue in valueCollection) { Console.WriteLine("Property Value: " + (ssortingng)propertyValue.ToSsortingng()); } Console.WriteLine(" "); catch ( InvalidOperationException iOe ) { dSearcher.Dispose(); } catch ( NotSupportedException nSe ) { } finally { if(sResults != null) sResults.Dispose(); } /* // Create new Excel file. var excelFile = new ExcelFile(); // Foreach DataTable, add new worksheet and insert data from DataTable into it. foreach (DataTable dataTable in dataSet.Tables) excelFile.Worksheets.Add(dataTable.TableName).InsertDataTable(dataTable, 0, 0, true); // Save Excel file to XLS format. excelFile.SaveXls(dataSet.DataSetName + ".xls"); * */ } } } } } 

Il doit s'agir de ceci:

  public void WriteEnsortinges(DirectoryEntry domainRoot) { DirectorySearcher searcher = new DirectorySearcher(domainRoot); searcher.PropertiesToLoad.Add("displayName"); searcher.PropertiesToLoad.Add("cn"); searcher.PropertiesToLoad.Add("department"); searcher.Filter = "(&(objectCategory=person))"; foreach (SearchResult result in searcher.FindAll()) { // Login Name Console.WriteLine(GetProperty(result, "cn")); // Display Name Console.WriteLine(GetProperty(result, "displayName")); // Department Console.WriteLine(GetProperty(result, "department")); } } private ssortingng GetProperty(SearchResult searchResult, ssortingng PropertyName) { if (searchResult.Properties.Contains(PropertyName)) { return searchResult.Properties[PropertyName][0].ToSsortingng(); } else { return ssortingng.Empty; } } 

Utilisez WriteEnsortinges avec une input racine pour searchr. Je les ai écrites dans la console et vous pouvez les placer dans une ligne Excel. Pour travailler avec excel, je vous recommand d'utiliser exellibrary .