Friday, April 24, 2009

Active directory queries

I had to spend lot of time trying to figure how to write queries on active directory using SSIS. I searched the internet for quite some time with no concrete examples. I did find examples using scripts but none for querying using ADO objects in SSIS. So I thought there might be others like me out there who might need some help .. so here goes

some of the Field names
- givenName = First name of user
- sn = Last name of user
- memberof = list of groups that the user belongs to
- cn = Display name
- objectGUID = Unique value for each record
- samAccountName = windows Login Name

The "FROM" clause would need to contain the complete domain controller name
so if you domain is called "corp.company.us.com", then your from clause would be like this
select field1, field2 from
FROM 'LDAP://DC=corp,DC=commpany,DC=us,DC=us'

if you want to fetch only the users you will need to specify a where clause like WHERE objectClass='user'

I had a requirement where I had to fetch users belonging to a particular group. I had a lot of problem finding this query. I finally learned that your where clause needs to have the whole heirarchy of the group specified. For eg, If you want to find all users in a Group called "Finance" your where clause needs to include

> memberOf = 'CN=Finance,DC=corp,DC=commpany,DC=us,DC=us'

Now if the department is within a Orgnization unit or OU called "AllUsers", then qour query needs to specify that too
> memberOf = 'CN=Finance,OU=AllUsers,DC=corp,DC=commpany,DC=us,DC=us'

Similarly you need to first check how the groups, users are created within your active directory and make changes to your query .. do remember to add the domain controller information too ..

Sample query finding all users within finance group where the finance group is plaed within a OU=Allusers

select cn
FROM 'LDAP://DC=corp,DC=commpany,DC=us,DC=us'
WHERE objectClass='user'
and memberOf = 'CN=Finance,OU=AllUsers,DC=corp,DC=commpany,DC=us,DC=us'

Hope this helps someone out there ..

No comments:

Post a Comment