Tuesday, September 27, 2011

Query AD using sql

declare @index int
declare @query nvarchar(2000)
declare @query_intreg nvarchar(2000)
declare @word varchar(1)

set @index = 65

while @index<91
begin
    set @word = char(@index)
    set @query = '''  select  mail,telephonenumber,cn,physicalDeliveryOfficeName,sAMAccountName,manager,adspath,Company,ipphone
                  from  ''''LDAP://AD.local/DC=AD,DC=local''''
                  WHERE sAMAccountName ='''''+@word+'*'''' AND Company = ''''*''''  ''  '


    set @query_intreg = 'select  mail,telephonenumber,cn,physicalDeliveryOfficeName,Company,0,sAMAccountName,adspath,manager,ipphone from  openquery(ADSI, '+@query+')'
   
    insert into #temp
    EXEC  sp_executesql @query_intreg
   
    set @index = @index +1
end

select * from #temp
drop table #temp

No comments:

Post a Comment