Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9045253
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T11:14:39+00:00 2026-06-16T11:14:39+00:00

Requirement: to pull a large amount of data (all users within an OU) from

  • 0

Requirement: to pull a large amount of data (all users within an OU) from active directory into a database table.

Method: I have been tasked with using SSIS to do this because there are other items to be completed as part of the same overnight task which are standard ETL things so this would just be another step in the process flow.

Code:

/*
Microsoft SQL Server Integration Services Script Task
Write scripts using Microsoft Visual C# 2008.
The ScriptMain is the entry point class of the script.
*/

using System;
using System.Data;
using System.Data.SqlClient;
using System.DirectoryServices;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;

namespace ST_dc256a9b209442c7bc089d333507abeb.csproj
{
    [System.AddIn.AddIn("ScriptMain", Version = "1.0", Publisher = "", Description = "")]
    public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
    {

        #region VSTA generated code
        enum ScriptResults
        {
            Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
            Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
        };
        #endregion

        /*
        The execution engine calls this method when the task executes.
        To access the object model, use the Dts property. Connections, variables, events,
        and logging features are available as members of the Dts property as shown in the following examples.

        To reference a variable, call Dts.Variables["MyCaseSensitiveVariableName"].Value;
        To post a log entry, call Dts.Log("This is my log text", 999, null);
        To fire an event, call Dts.Events.FireInformation(99, "test", "hit the help message", "", 0, true);

        To use the connections collection use something like the following:
        ConnectionManager cm = Dts.Connections.Add("OLEDB");
        cm.ConnectionString = "Data Source=localhost;Initial Catalog=AdventureWorks;Provider=SQLNCLI10;Integrated Security=SSPI;Auto Translate=False;";

        Before returning from this method, set the value of Dts.TaskResult to indicate success or failure.

        To open Help, press F1.
    */

        public void Main()
        {
        //Set up the AD connection;
        using (DirectorySearcher ds = new DirectorySearcher())
            {
            //Edit the filter for your purposes;
            ds.Filter = "(&(objectClass=user)(|(sAMAccountName=A*)(sAMAccountName=D0*)))";
            ds.SearchScope = SearchScope.Subtree;
            ds.PageSize = 1000;
            //This will page through the records 1000 at a time;

            //Set up SQL Connection
            string sSqlConn = Dts.Variables["SqlConn"].Value.ToString();
            SqlConnection sqlConnection1 = new SqlConnection(sSqlConn);
            SqlCommand cmd = new SqlCommand();
            SqlDataReader reader;
            cmd.CommandType = CommandType.Text;
            cmd.Connection = sqlConnection1;

            //Read all records in AD that meet the search criteria into a Collection
            using (SearchResultCollection src = ds.FindAll())
                {
                //For each record object in the Collection, insert a record into the SQL table
                foreach (SearchResult results in src)
                    {
                    string sAMAccountName = results.Properties["sAMAccountName"][0].ToString();
                    //string objectCategory = results.Properties["objectCategory"][0].ToString();
                    string objectSid = results.Properties["objectSid"][0].ToString();
                    string givenName = results.Properties["givenName"][0].ToString();
                    string lastName = results.Properties["sn"][0].ToString();
                    string employeeID = results.Properties["employeeID"][0].ToString();
                    string email = results.Properties["mail"][0].ToString();

                    //Replace any single quotes in the string with two single quotes for sql INSERT statement
                    objectSid = objectSid.Replace("'", "''");
                    givenName = givenName.Replace("'", "''");
                    lastName = lastName.Replace("'", "''");
                    employeeID = employeeID.Replace("'", "''");
                    email = email.Replace("'", "''");

                    sqlConnection1.Open();
                    cmd.CommandText = "INSERT INTO ADImport (userName, objectSid, firstName, lastName, employeeNo, email) VALUES ('" + sAMAccountName + "','" + objectSid + "','" + givenName + "','" + lastName + "','" + employeeID + "','" + email + "')";
                    reader = cmd.ExecuteReader();

                    string propertyName = "Description"; //or whichever multi-value field you are importing
                    ResultPropertyValueCollection valueCollection = results.Properties[propertyName];

                    //Iterate thru the collection for the user and insert each value from the multi-value field into a table
                    foreach (String sMultiValueField in valueCollection)
                        {
                        string sValue = sMultiValueField.Replace("'","''"); //Replace any single quotes with double quotes
                        //sqlConnection1.Open();
                        cmd.CommandText = "INSERT INTO ADImport_Description (userName, objectSid, objectDescription) VALUES ('" + sAMAccountName + "','" + objectSid + "','" + sValue + "')";
                        reader = cmd.ExecuteReader();
                        //sqlConnection1.Close();
                        }
                    sqlConnection1.Close();
                    } 
                } 
            } 
            Dts.TaskResult = (int)ScriptResults.Success;
        }
    }
}

I expect a lot of you will recognise this as being basically the same as code from this post from the data queen: http://dataqueen.unlimitedviz.com/2012/09/get-around-active-directory-paging-on-ssis-import/ which I’ve adapted for my purposes as I was running into a lot of issues with the code I’d written myself.

This is all in one script task on its own at the moment. Yes, I’ve added in the relevant references so they are there.

Problem: when I run the script task in SSIS (on its own to avoid any chance of the other parts of the package interfering) I get:

Error: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
at System.Collections.ArrayList.get_Item(Int32 index)
at System.DirectoryServices.ResultPropertyValueCollection.get_Item(Int32 index)
at ST_dc256a9b209442c7bc089d333507abeb.csproj.ScriptMain.Main()

Please, anyone, any ideas????????

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T11:14:40+00:00Added an answer on June 16, 2026 at 11:14 am

    All credit to @billinkc (billinkc – if you add an answer I will accept it instead of this one but I don’t like leaving questions unanswered so adding your answer for now as you haven’t added an answer over the last week)

    “My guess is the failing line is string employeeID = results.Properties[“employeeID”][0].ToString(); as you are probably getting things like Service accounts which won’t have an employeeID defined. Copy your code into a proper .NET project (I like console) and step through with the deubgger to find the line number. – billinkc”

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Requirement: Requirement is to pull (execute()) a set of data sources. The pulls are
I have a requirement to pull a total # of product quantity from an
I am currently pulling data into MongoDB, and will later need to pull this
Requirement : Should update MySQL database with that of MS Sql Server updates which
Requirement is like: I want to take input values from user in frame but
My requirement is to use 2 tables and 1 chart to visualize my data
Current requirement is on button click I am getting a json data through ajax
My requirement is parts of text should be displayed as bold and colored within
I have a crystal report that is connecting to a database. The datatable from
I have a requirement where certain code cannot be seen by all developers, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.