Querying AD from SQL Server
You might be better off and find it easier to query Active Directory with a CLR stored procedure or CLR function. See this walkthrough on how to do so, as this is how I've done it in the past. Also someone else made one already you can look into (but be careful installing CLR assemblies from outside sources).