简体   繁体   English

使用C#将广告信息提取到Excel中

[英]Pulling AD info into Excel using C#

i'm new to C# and am looking to pull data from a Active Directory using C# and output it into an excel file. 我是C#的新手,正在寻求使用C#从Active Directory中提取数据并将其输出到excel文件中。 I've looked at a few tutorials and am confused at how to do this. 我看了一些教程,却对如何做到这一点感到困惑。 I'm looking to obtain userID, Department, and Name. 我正在寻找用户ID,部门和名称。 how would I do this using a .FindAll() functionality? 我将如何使用.FindAll()功能执行此操作?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.DirectoryServices;

namespace ADquery
 {
        class UserLookUp
    {
    static void Main(string[] args)
    {
        Console.WriteLine(retrieveUsers());
        Console.ReadLine();
    }

    public static string retrieveUsers()
    {

        string 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].ToString() != null)
                {

                    //pull info desired
                    ResultPropertyValueCollection valueCollection =
                    searchResults.Properties["manager"];

                    foreach (Object propertyValue in valueCollection)
                    {
                        Console.WriteLine("Property Value: " + (string)propertyValue.ToString());
                    }

                    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"); *

       */

                }
            }
    }
}
}

It must be something like this: 一定是这样的:

    public void WriteEntries(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 string GetProperty(SearchResult searchResult, string PropertyName)
    {
        if (searchResult.Properties.Contains(PropertyName))
        {
            return searchResult.Properties[PropertyName][0].ToString();
        }
        else
        {
            return string.Empty;
        }
    }

Use WriteEntries with a root entry to search in. I wrote them in console and you can put them in an excel row. 使用带有根条目的WriteEntries进行搜索。我在控制台中编写了它们,您可以将它们放在excel行中。 For working with excel I recommend you to use exellibrary . 对于使用excel,我建议您使用exelbrary

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM