This is a simple method to load a SQL database with active directory account information without running into the 1000 record limit. You will need to change the SERVERNAME, PORT, DATABASENAME, USERID, AND PASSWORD values in the connection strings to your own values. You will also need to change the LDAP connection string values ('LDAP://SERVERNAME/ou=OUName,dc=host,dc=domain,dc=tld') to your own values.
-- Create a table to hold the OU paths.
create table dbo.[OU](
ADsPath ntext null,
rec_id int identity(1, 1) not null
)
on [PRIMARY] textimage_on [PRIMARY];
-- Set up an OLEDB connection to Microsoft Directory
-- Services provider and query LDAP to load the OU table.
-- Alternately, enter the OU values into the table manually
-- in the form
-- LDAP://servername/OU=ou,DC=host,DC=domain,DC=tld
-- to extract from specific OUs
select ADsPath
from 'LDAP://SERVERNAME/ou=OUName,dc=host,dc=domain,dc=tld'
where objectCategory = 'organizationalUnit';
-- Create a table to hold the accounts.
set ansi_nulls, quoted_identifier, ansi_padding on;
go
create table dbo.Accounts(
manager varchar(512) null,
company varchar(512) null,
department varchar(512) null,
title varchar(512) null,
facsimileTelephoneNumber varchar(512) null,
info varchar(512) null,
ipPhone varchar(512) null,
mobile varchar(512) null,
pager varchar(512) null,
homePhone varchar(512) null,
HomeDrive varchar(512) null,
HomeDirectory varchar(512) null,
ScriptPath varchar(512) null,
ProfilePath varchar(512) null,
userWorkstations varchar(512) null,
samAccountName varchar(512) null,
userPrincipalName varchar(512) null,
c varchar(512) null,
postalCode varchar(512) null,
st varchar(512) null,
l varchar(512) null,
streetAddress varchar(512) null,
mail varchar(512) null,
telephoneNumber varchar(512) null,
physicalDeliveryOfficeName varchar(512) null,
displayName varchar(512) null,
sn varchar(512) null,
initials varchar(512) null,
givenName varchar(512) null)
on [PRIMARY];
go
---------------------------------------
' Run a vbscript to load the Accounts table.
Option Explicit
Dim adoCommand, _
adoConnection, _
strBase, _
strFilter, _
strAttributes
Dim objRootDSE, _
strDNSDomain, _
strQuery, _
adoRecordset
Dim givenName, _
initials, _
sn, _
displayName, _
physicalDeliveryOfficeName, _
telephoneNumber, _
mail, _
streetAddress, _
l, _
st, _
postalCode, _
c, _
userPrincipalName, _
samAccountName, _
userWorkstations, _
ProfilePath, _
ScriptPath, _
HomeDirectory, _
HomeDrive, _
homePhone, _
pager, _
mobile, _
ipPhone, _
info, _
facsimileTelephoneNumber, _
title, _
department, _
company, _
manager
Dim sqlCommand, _
sqlConnection
Dim adPath, _
adSql, _
adConnection
' Setup ADO objects.
Set adoCommand = CreateObject("ADODB.Command")
Set adoConnection = CreateObject("ADODB.Connection")
Set sqlConnection = CreateObject("ADODB.Connection")
Set sqlCommand = CreateObject("ADODB.Command")
Set adPath = CreateObject("ADODB.Recordset")
Set adConnection = CreateObject("ADODB.Connection")
Set adSql= CreateObject("ADODB.Command")
'OU=OUName,DC=host,DC=domain,DC=tld
adSql.CommandText = "select adspath from OU"
adConnection.Open "Driver={SQL Server};server=SERVERNAME,PORT;" & _
"database=DATABASENAME_In;uid=USERID;pwd=PASSWORD;"
adSql.ActiveConnection = adConnection
Set adPath = adSql.Execute
Do Until adpath.EOF
adoConnection.Provider = "ADsDSOObject"
adoConnection.Open "Active Directory Provider"
adoCommand.ActiveConnection = adoConnection
' Search entire Active Directory domain.
strDNSDomain = adPath("ADsPath")
strBase = "<"& strDNSDomain &">"
' Filter on user objects.
strFilter = "(&(objectClass=user))"
' Comma delimited list of attribute values to retrieve.
strAttributes = "givenName," & _
"initials," & _
"sn," & _
"displayName," & _
"physicalDeliveryOfficeName," & _
"telephoneNumber," & _
"mail," & _
"streetAddress," & _
"l," & _
"st," & _
"postalCode," & _
"c," & _
"userPrincipalName," & _
"samAccountName," & _
"userWorkstations," & _
"ProfilePath," & _
"ScriptPath," & _
"HomeDirectory," & _
"HomeDrive," & _
"homePhone," & _
"pager," & _
"mobile," & _
"ipPhone," & _
"info," & _
"facsimileTelephoneNumber," & _
"title," & _
"department," & _
"company," & _
"manager,"
' Construct the LDAP syntax query.
strQuery = strBase & ";" & strFilter & ";" & strAttributes
adoCommand.CommandText = strQuery
adoCommand.Properties("Page Size") = 1000
adoCommand.Properties("Timeout") = 30
adoCommand.Properties("Cache Results") = False
' Run the query.
Set adoRecordset = adoCommand.Execute
' Enumerate the resulting recordset.
Do Until adoRecordset.EOF
' Retrieve values and display.
givenName= adoRecordset.Fields("givenName").Value
initials= adoRecordset.Fields("initials").Value
sn=adoRecordset.Fields("sn").Value
displayName= adoRecordset.Fields("displayname").Value
physicalDeliveryOfficeName = adoRecordset.Fields("physicalDeliveryofficename").Value
telephoneNumber= adoRecordset.Fields("telephonenumber").Value
mail=adoRecordset.Fields("mail").Value
streetAddress=adoRecordset.Fields("streetaddress").Value
l=adoRecordset.Fields("l").Value
st=adoRecordset.Fields("st").Value
postalCode=adoRecordset.Fields("postalcode").Value
c=adoRecordset.Fields("c").Value
userPrincipalName=adoRecordset.Fields("userprincipalname").Value
samAccountName=adoRecordset.Fields("samAccountName").Value
userWorkstations=adoRecordset.Fields("userworkstations").Value
ProfilePath=adoRecordset.Fields("profilepath").Value
ScriptPath=adoRecordset.Fields("Scriptpath").Value
HomeDirectory=adoRecordset.Fields("Homedirectory").Value
HomeDrive=adoRecordset.Fields("homedrive").Value
homePhone=adoRecordset.Fields("homephone").Value
pager=adoRecordset.Fields("pager").Value
mobile=adoRecordset.Fields("mobile").Value
ipPhone=adoRecordset.Fields("ipphone").Value
info=adoRecordset.Fields("Info").Value
facsimileTelephoneNumber=adoRecordset.Fields("facsimileTelephonenumber").Value
title=adoRecordset.Fields("title").Value
department=adoRecordset.Fields("department").Value
company=adoRecordset.Fields("company").Value
manager=adoRecordset.Fields("manager").Value
SQLConnection.Open "Driver={SQL Server};server=SERVERNAME,PORT;" & _
"database=DATABASENAME_In;uid=USERID;pwd=PASSWORD;"
Set sqlCommand.ActiveConnection = sqlConnection
SQLCommand.CommandText = "SET QUOTED_IDENTIFIER OFF " & _
"INSERT INTO " & _
"Accounts " & _
"(samAccountName," & _
"givenName," & _
"initials," & _
"sn," & _
"displayName," & _
"physicalDeliveryOfficeName," & _
"telephoneNumber," & _
"mail," & _
"streetAddress," & _
"l," & _
"st," & _
"postalCode," & _
"c," & _
"userPrincipalName," & _
"userWorkstations," & _
"ProfilePath," & _
"ScriptPath," & _
"HomeDirectory," & _
"HomeDrive," & _
"homePhone," & _
"pager," & _
"mobile," & _
"ipPhone," & _
"info," & _
"facsimileTelephoneNumber," & _
"title," & _
"department," & _
"company," & _
"manager," & _
"VALUES(" & _
chr(34) & sAMAccountName & chr(34) & "," & _
chr(34) & givenName & chr(34) & "," & _
chr(34) & initials & chr(34) & "," & _
chr(34) & sn & chr(34) & "," & _
chr(34) & displayName & chr(34) & "," & _
chr(34) & physicalDeliveryOfficeName & chr(34) & "," & _
chr(34) & telephoneNumber & chr(34) & "," & _
chr(34) & mail & chr(34) & "," & _
chr(34) & streetAddress & chr(34) & "," & _
chr(34) & l & chr(34) & "," & _
chr(34) & st & chr(34) & "," & _
chr(34) & postalCode & chr(34) & "," & _
chr(34) & c & chr(34) & "," & _
chr(34) & userPrincipalName & chr(34) & "," & _
chr(34) & userWorkstations & chr(34) & "," & _
chr(34) & ProfilePath & chr(34) & "," & _
chr(34) & ScriptPath & chr(34) & "," & _
chr(34) & HomeDirectory & chr(34) & "," & _
chr(34) & HomeDrive & chr(34) & "," & _
chr(34) & homePhone & chr(34) & "," & _
chr(34) & pager & chr(34) & "," & _
chr(34) & mobile & chr(34) & "," & _
chr(34) & ipPhone & chr(34) & "," & _
chr(34) & info & chr(34) &"," & _
chr(34) & facsimileTelephoneNumber & chr(34) & "," & _
chr(34) & title & chr(34) & "," & _
chr(34) & department & chr(34) & "," & _
chr(34) & company & chr(34) & "," & _
chr(34) & manager & chr(34) & ")"
sqlCommand.CommandType = 1
sqlCommand.Execute
sqlConnection.Close
' Move to the next record in the recordset.
adoRecordset.MoveNext
Loop
adoRecordset.Close
adoConnection.Close
' Move to the next record in the recordset.
adPath.MoveNext
Loop
' Clean up.
adPath.Close
adConnection.Close