简体   繁体   English

Active Directory数据进入SQL表

[英]Active Directory data into SQL table

How would I extract Active Directory info (Username, first name, surname) and populate an SQL table with the results? 如何提取Active Directory信息(用户名,名字,姓氏)并用结果填充SQL表?

Many thanks 非常感谢

Scott 史考特

The way we do this for a LARGE AD environment: 我们在大型广告环境中执行此操作的方式:

  1. Nightly batch process that runs AdFind (freeware tool) to execute an LDAP query and dump it out to CSV files 每晚运行AdFind (免费工具)的批处理过程,以执行LDAP查询并将其转储为CSV文件
  2. BCP (built-in SQL command line tool) to bulk import the CSV files into import tables in the SQL database BCP (内置SQL命令行工具),用于将CSV文件批量导入到SQL数据库中的导入表中
  3. Stored procedure (executed with osql ) to take the data from the import table and add/update records in the main tables 存储过程(用osql执行)以从导入表中获取数据并在主表中添加/更新记录

We pull 145k users, 80k groups, 130k computers from 10 domains in about 2 hours from start to finish. 从开始到结束,我们在大约2小时内从10个域中吸引了145k用户,80k组,13万台计算机。 This includes pulling accurate LastLogon information for the users and computers which requires you to hit each domain controller. 这包括为用户和计算机提取准确的LastLogon信息,这需要您单击每个域控制器。 Without that, the process takes about 30 minutes. 否则,该过程大约需要30分钟。

If you just need it in SQL, I'm using the code below 如果您只需要SQL中的代码,则使用下面的代码

INSERT...
SELECT A.SAMAccountName, A.Mail,  A.displayName  FROM
    (SELECT * FROM OpenQuery(ADSI, 'SELECT title, displayName, sAMAccountName, givenName, telephoneNumber, facsimileTelephoneNumber, sn, userAccountControl,mail  
    FROM ''LDAP://domain.ro/DC=domain,DC=ro'' where objectClass = ''User''')
    WHERE (sn is not null) and (givenName is not null) and (mail is not null) )A

where ADSI is a linked server created based on this: http://msdn2.microsoft.com/en-us/library/aa772380(VS.85).aspx 其中ADSI是基于此创建的链接服务器: http : //msdn2.microsoft.com/zh-cn/library/aa772380(VS.85).aspx

If you're on .NET 3.5, I would use the new System.DirectoryServices.AccountManagement namespace for this. 如果您使用的是.NET 3.5,我将为此使用新的System.DirectoryServices.AccountManagement命名空间。

Learn about it here: 在这里了解它:

Managing Directory Security Principals in the .NET Framework 3.5 在.NET Framework 3.5中管理目录安全主体

Basically, you'd set up a container (a PrincipalContext ) and then enumerate the users you want to deal with. 基本上,您将设置一个容器( PrincipalContext ),然后枚举要处理的用户。 Loop over those and extract the info you need, and feed that into SQL Server. 遍历这些信息并提取所需的信息,然后将其提供给SQL Server。

There are different ways to do that. 有不同的方法可以做到这一点。 I use PHP to get data out of our Active Directory.Take a look at the chapter " Lightweight Directory Access Protocol " in the PHP Documentation. 我使用PHP从Active Directory中获取数据。请查看PHP文档中的“ 轻型目录访问协议 ”一章。 It's also easy to populate a database using PHP, eg MySQL or Microsoft SQL Server . 使用PHP填充数据库也很容易,例如MySQLMicrosoft SQL Server

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

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