473,396 Members | 2,090 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Retrieving information from Active Directory through OLE-DB

In this brief tutorial I'll describe how you retrieve information from
an Active Directory through the OLE-DB extension. While it is possible
to use the LDAP extension to achieve the same goal, as you will see
using Microsoft's OLE-DB provider is much easier.

You will need to download and install the OLE-DB extension. Here's the
location once again:
See my earlier tutorial on Indexing Service for set-up instructions.

The first thing we do is open a connect to the AD provider:

$link = oledb_open("Provider=ADSDSOObject");

If the web server is not on the network, then we'd need to provide the
name and password of an account on the network:

$link = oledb_open("Provider=ADSDSOObject;
User ID*****@somewhere.net;

Once that's done we can query the directory. A nicety of the Active
Directory Service OLE-DB provider is that it understands SQL, so you
don't need to learn a new query language. To retrieve a list of e-mail
addresses, we'd do the following:


$link = oledb_open("Provider=ADSDSOObject");
$table= 'LDAP://domain';
$sql = "SELECT cn, mail
FROM '$table'
WHERE objectClass = 'user'
AND objectCategory = 'person'
AND mail = '*' ";
while($row = oledb_fetch_assoc($res)) {


The table name used in the FROM clause is the ADsPath of the node from
which we start the search. In the example we just use the NT domain
name to search the whole directory. Depending on the complexity of your
directory you might want to specify something a little more

The objectClass = 'user' AND objectCategory = 'person' criteria
specifies that we want records of users who are actually people. The
mail = '*' part ensures that we don't get records with no e-mail
address. The "IS NOT NULL" syntax is not supported.

The result of the query would look something like this:

array(3) {
string(16) "bt*****@guru.be"
string(14) "Bob Thomas"

cn is the "common name" of the LDAP object. For a person it's first
name plus last name. If we'd asked for displayName, then we'd have
gotten "Thomas, Bob" instead. mail is the person's e-mail address. The
zeroth element in the array is the index of the record. For some reason
the OLE-DB provider insists on returning it. It can be ignored.

Now, suppose we want to get the e-mail addresses of people in the
office whose last name starts with the letter L. To do this we do a
wildcard match on the sn (short for surname) column:

$sql = "SELECT displayName, sn, mail
FROM '$table'
WHERE objectClass = 'user'
AND objectCategory = 'person'
AND sn = 'L*'
AND mail = '*' ";

To find the telephone number of a particular person, we do an exact
match on the sn and givenName (i.e. first name) columns:

$sql = "SELECT givenName, sn, telephoneNumber
FROM '$table'
WHERE objectClass = 'user'
AND objectCategory = 'person'
AND sn = 'Henderson'
AND givenName = 'Emmanuel' ";

In addition to personal information, Active Directory also holds
information about computing resources on the network. To get a list of
computers and the operation system installed, we'd use this query:

$sql = "SELECT cn, operatingSystem, operatingSystemServicePack
FROM '$table'
WHERE objectClass = 'computer' ";

To get a list of printers and their physical location:

$sql = "SELECT printerName, physicalLocationObject
FROM '$table'
WHERE objectClass = 'printQueue' ";

Obviously if no had bothered to enter the location of the printer at
some point, that wouldn't be available. Active Directory isn't magic
after all. It's simply a database and you can only get what has once
been put it.

The complete Active Directory schema can be found here:

Aug 27 '06 #1
0 5392

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

by: Mario Rodriguez | last post by:
Hi People, I'm try to access an Active Directory using both DirectoryEntry and DirectorySearcher (.NET DirectoryServices) and OLEDB Active Directory, in all cases I'm receiving this error: No...
by: CJM | last post by:
I'm trying to retrieve the users email address via ADSI in ASP. So far, I have the following code: Set oUser = GetObject("WinNT://domain/" & Request.Form("UID") & ",user") Response.Write...
by: Steve | last post by:
Can anyone tell me the preferred method for writing and retrieving persistent information using .Net. Specifically, I am referring to information that you used to see in registry keys or .ini...
by: Paul Kennedy | last post by:
I have a situation where I am utilizing code from MSDN to insert XLS files into a Microsoft Access Table in a column of Ole Object using VBA and DAO. That code also provides me with a method to...
by: Frank | last post by:
Thanks a lot, I'll check it out straight away :-) Regards Frank >-----Original Message----- >Yes, very much possible. You can read the active directory >information using...
by: tangus via DotNetMonster.com | last post by:
Hello all, I'm really struggling with getting some Active Directory code to work in ASP.NET. Can you please provide assistance? I am executing the following code: Dim enTry As DirectoryEntry =...
by: Nathan Franklin | last post by:
hello list, i am writing a pocket pc project. just this morning i opened up my project and I got the following error.. error retrieving information from user datastore. and then the...
by: rh | last post by:
Hi, I need to retrieve all usernames within an active directory security group (including sub-groups) into my C# application. Is there a relatively simple way of doing this? (I'm hoping for a...
by: fabiomoggi | last post by:
Hello Guys, I am developing a web application to manage Active Directory resources, and one of my tasks is to map Organizational Units hierarchy into a SQL Server database. Let's suppose that I...
by: lairpr74 | last post by:
Hi, I got a console application that updates active directory users, the information to be updated in active directory comes from a database table. This is my problem: right now the application...
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.