Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Active Directory query for username, first name, last name and email

Pardon my ignorance, I do not know much about AD (let lone querying with AD or googling for that). I would like to get a list of all the users in a particular domain, their first name, last name and email ids. Would a network admin (or help desk in my case) be able to do that ? My other option: I have the usernames in an excel sheet, Full name in another text file (amongst other data - say XXXyy , FirstName LastName- I would have to split,parse it to extract the name) and email in another file and none of them are in order. There might be some missing data too :(

What would be the best way to go about it with Querying AD ?

Edit:May be I should be more specific. If I was seeing what my network admin would be doing to get me this info, what would he be doing ?

like image 298
ram Avatar asked Dec 17 '09 22:12

ram


2 Answers

Active Directory exposes query interface via OLE DB and ADO. The provider is "ADsDSOObject", the query syntax goes like this:

<LDAP://mydomain.com>;(objectType=user);givenname,sn

Perversely, the URL schema name LDAP must be capitalized.

Excel does not have a built-in ADO client, unless you code in VBA.

UPDATE: wrote a simple JavaScript query script for you:

var conn = new ActiveXObject("ADODB.Connection");
conn.Open("Provider=ADsDSOObject");
var rs = conn.Execute("<LDAP://your-domain.com>;(objectClass=user);sn,givenname");
var i;
if(!rs.EOF)
{
    rs.MoveFirst();
    while(!rs.EOF)
    {

        WScript.Echo(rs.Fields.Item("givenname")+","+rs.Fields.Item("sn")+"\n");
        rs.MoveNext();
    }
}

It queries the fiest and last name of all users in your domain. Place your domain name in the third line. Then save it as a .js file, and execute thusly:

cscript adquery.js >a.txt

And you'll end up with a text file called a.txt, with the names of your users, comma-separated. Import it into Excel or something.

In Excel, if you are willing to mess with macros, you can write a VBA function against ADO that performs the same query. Or use .NET's DirectorySearcher, recent versions of Excel let you consume .NET objects.

like image 183
Seva Alekseyev Avatar answered Jan 07 '23 09:01

Seva Alekseyev


If you're using the .NET platform, I would suggest looking into the System.DirectoryServices namespace, which "provides easy access to Active Directory Domain Services from managed code."

MSDN also provides code samples for performing common tasks using System.DirectoryServices, available in both VB and C#. If you're familiar with one of these languages, you should hopefully be able to glean what you need (at least to get started, and then perhaps be able to ask other, more specific questions here on SO) from these examples.

Hope this helps!

like image 35
Donut Avatar answered Jan 07 '23 09:01

Donut