Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Querying Active Directory with "SQL"?

I just wonder if anyone knows or made a wrapper around Active Directory to be able to easily query it in .net? Kind of like "LINQ-to-ActiveDirectory" or some SQL Dialect, i.e. to be able to do "SELECT DISTINCT(DEPARTMENT) FROM /Users/SomeOU/AnotherOU" or "SELECT user FROM domain" or whatever.

As far as I know, it is possible to query WMI and IIS in a "SQLesque" way, i just wonder if something similar is possible for Active Directory as well, without having to learn yet another Query Language (LDAP)?

like image 397
Michael Stum Avatar asked Sep 18 '08 07:09

Michael Stum


People also ask

Can you query Active Directory from SQL?

You could query ADSI just by using the domain name and domain container. In this article, I am going to use the following OU and DC. Note: Please replace the OU, DC and CN information mentioned here with your company's OU, CN and DC. The first method to query Active Directory from SQL Server is by using OpenRowSet.

What is Active Directory in SQL?

Active Directory: It is a container that consists of organization units for all users, their credentials, groups. All users must authenticate themselves to use an organization resource.

How does SQL Server connect to Active Directory?

Open SQL Server Management Studio and connect to an instance of SQL Server. In the Object Explorer, expand the node for the SQL Server database. In the Server Objects node, right-click Linked Servers and click New Linked Server.


1 Answers

LINQ to Active Directory implements a custom LINQ query provider that allows querying objects in Active Directory. Internally, queries are translated into LDAP filters which are sent to the server using the System.DirectoryServices .NET Framework library.

http://www.codeplex.com/LINQtoAD

Sample (from the site):

// NOTE: Entity type definition "User" omitted in sample - see samples in release.

var users = new DirectorySource<User>(ROOT, SearchScope.Subtree);
users.Log = Console.Out;

var res = from usr in users
          where usr.FirstName.StartsWith("B") && usr.Office == "2525"
          select new { Name = usr.FirstName + " " + usr.LastName, usr.Office, usr.LogonCount };

foreach (var u in res)
{
    Console.WriteLine(u);
    u.Office = "5252";
    u.SetPassword(pwd);
}

users.Update();
like image 193
Espo Avatar answered Oct 04 '22 16:10

Espo