Using VB.NET in an SSIS package, how do I populate an SQL Server table with the users in multiple Active Directory domains (in the same forest)?
相关问题
- 'System.Threading.ThreadAbortException' in
- how to use special characters like '<'
- Active Directory on-prem Manager
- C# to VB - How do I convert this anonymous method
- Scaling image for printing
相关文章
- vb.net 关于xps文件操作问题
- Checking for DBNull throws a StrongTypingException
- getting user details from AD is slow
- Using the typical get set properties in C#… with p
- SSIS solution on GIT?
- Load a .NET assembly from the application's re
- C# equivalent of VB DLL function declaration (Inte
- What other neat tricks does the SpecialNameAttribu
Linked Server approach:
Set up a linked server to connect to Active Directory:
Here is one possible option using Linked Server on SQL Server that does not actually require
VB.NET
, if that is an option for you. The queries given below are only for syntax references. Please read the links for proper usage of these SQL Server objects.You could set up a linked server on SQL Server to connect to the Active Directory using
OLE DB Provider for Microsoft Directory Services
. Below is a sample script that would set up a linked server and map it with a login that has access to the Active directory.Usage of sp_addlinkedserver (Transact-SQL)
Usage of sp_addlinkedsrvlogin (Transact-SQL)
Sample script to set up linked server:
Use OpenQuery to query Active Directory:
You can then use OpenQuery to query the Active Directory for user information. Read the below link on MSDN for more information on how to formulate the query.
Search Active Directory - Distributed Query
Sample script to query Active Directory:
How to use this data in SSIS package:
You could set up this query as an SQL Server view and then call the view from OLE DB Source available on Data Flow Task within SSIS package and then redirect the output to an OLE DB Destination to populate the database tables.
I went the route of using a query similar to Siva's approach except instead of a linked server query, I use the ADSI provider. Active Directory SSIS Data Source
The code in the referenced post will pull all users from a given domain. After converting from NTEXT to TEXT to String, I then use the distinguished name and the DirectoryServices assembly to enumerate all the first order groups for a user via their distinguished name. It does not address nested groups. Conveniently enough, that post covered a 2005 SSIS implementation so the logic is in VB.
Script Component (VB.NET) with System.DirectoryServices
Here is a sample logic that import Active Directory users information from one domain into database table with the help of VB.NET in Script Component configured as Source. This sample was tested in SSIS 2012 but should work in SSIS 2008 and above. This logic will not work in SSIS 2005 because the namespace
System.DirectoryServices.AccountManagement
was introduced only in .NET framework 3.5 and SSIS 2005 uses .NET Framework 2.0Create an SSIS package. This sample uses SSIS 2012.
Create an
OLEDB Connection Manager
that would connect to the SQL Server database. If you created a data source, add the data source to the package's connection manager tab.Drag and drop a
Data Flow Task
onto the Control Flow tab.Double-click the
Data Flow Task
to switch to the Data Flow tab.Drag and drop a
Script Component
onto the Data Flow tab.Check
Source
on the Select Script Component Type dialog and click OK.Double-click the Script Component to open the Script Transformation Editor. Click
Inputs and Outputs
tab page.Rename the Output to ActiveDirectory to give a meaningful name.
Select Output Columns and click Add Column to add each of the below mentioned columns. This is only to illustrate this example. You might need to add columns of your preference.
Column definition within script component
After defining the columns, click
Script
tab pageChange the ScriptLanguage to
Microsoft Visual Basic 2010
On the Solution Explorer, right-click the Script Component project and click
Add Reference...
. Add references to the following namespaces.Namespaces to be referenced in the script component
<Your domain name goes here>
with your appropriate domain name. The code initializes PrincipalContext and PrincipalSearcher objects inPreExecute
method and then disposes them inPostExecute
method. CreateNewOutputRows method loops through each of the row found in AD to fetch the user attributes information.Script component code (VB.NET)
Close the Script Transformation Editor.
Drag and drop an OLE DB Destination onto the Data Flow tab. Connect the Script component to the OLE DB destination to redirect the source output. Select the appropriate OLE DB Connection Manager and the table where the data should be inserted into.
Ways to improve this approach:
This sample provides loading information from only one domain. If you have multiple domains, you could stored them in a table. Fetch information of all the domain lists and use
Foreach Loop Container
available on the Control Flow to loop through each domain and get the users information using the above mentioned approach. May be there is also a better way to do this within VB.NET.Active Directory User Attributes
You can find the complete list of Active Directory user attributes in the below MSDN link. You need to click the links to find the LDAP-Display-Name.
All Attributes (Windows)
Here is another link that might help to get the user object attributes
User Object Attributes (Windows)