Query on Active Directory Organizational Units

  • I'm attempting to query ADSI to get the user's Organizational Unit.

    I run the following and it works perfect but I can't figure out how to add the OU:

    SELECT

    department, sn + '', '' + givenname AS name, CAST(objectGuid as uniqueidentifier) as UserGuid

    FROM OPENQUERY(ADSI,

    'SELECT sn,givenname,department,objectGuid

    FROM ''LDAP://OU=abccorp,DC=abccorp,DC=abccorp,DC=com''

    ')

    abccorp is the top OU.  All the users are in OU's under abccorp.

    Now, I know that we are in a tree structure so I'm willing to settle for lowest-level OU.

    Any help will be greatly appreciated!

  • I think its just

    SELECT department, sn + '', '' + givenname AS name, CAST(objectGuid as uniqueidentifier) as UserGuid, ou

    FROM OPENQUERY(ADSI,

    'SELECT sn,givenname,department,objectGuid, ou

    FROM ''LDAP://OU=abccorp,DC=abccorp,DC=abccorp,DC=com''

    ')

    it should be noted that some things cannot be retieved for example a users group memebership as the AD returns an arrya for this attribute and sql server doesn't support arrays.

    To find out all the attributes for an onject and their ldap dispaly name you can look at the ad schema pages on msdn

    http://msdn2.microsoft.com/en-us/library/ms680938.aspx

    hth

    David

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply