Active Directory data into SQL table

2019-01-14 18:08发布

How would I extract Active Directory info (Username, first name, surname) and populate an SQL table with the results?

Many thanks

Scott

4条回答
老娘就宠你
2楼-- · 2019-01-14 18:29

If you just need it in SQL, I'm using the code below

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

查看更多
兄弟一词,经得起流年.
3楼-- · 2019-01-14 18:35

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
  2. BCP (built-in SQL command line tool) to bulk import the CSV files into import tables in the SQL database
  3. Stored procedure (executed with osql) to take the data from the import table and add/update records in the main tables

We pull 145k users, 80k groups, 130k computers from 10 domains in about 2 hours from start to finish. This includes pulling accurate LastLogon information for the users and computers which requires you to hit each domain controller. Without that, the process takes about 30 minutes.

查看更多
萌系小妹纸
4楼-- · 2019-01-14 18:39

If you're on .NET 3.5, I would use the new System.DirectoryServices.AccountManagement namespace for this.

Learn about it here:

Managing Directory Security Principals in the .NET Framework 3.5

Basically, you'd set up a container (a PrincipalContext) and then enumerate the users you want to deal with. Loop over those and extract the info you need, and feed that into SQL Server.

查看更多
叼着烟拽天下
5楼-- · 2019-01-14 18:55

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. It's also easy to populate a database using PHP, e.g. MySQL or Microsoft SQL Server.

查看更多
登录 后发表回答