使用 SSIS 填充 Active Directory

Populate Active Directory with SSIS

我看过很多从 AD 中提取数据的帖子,但我需要更新 AD,但找不到我要找的东西。我的公司一直在使用第三方供应商 (intelli-desk) 来安置部门、经理和 phone etx。多年的信息。 AD 仅用于创建用户名和分配域访问权限。我们知道需要使用从 intelli-desk 到 AD 的信息更新 AD。我已经构建了一个比较 AD 和 intelli-desk 数据库的查询,并希望获取该查询的结果,其中 sn 和 givenName 匹配并更新 AD 中的分机、部门等。我在想 SSIS 包可能是执行此操作的最佳方式,这样它就可以成为一项预定的工作,因为我认为我们不会停止使用第 3 方软件。有没有人对使用 SSIS 有任何建议,如果有的话,你会怎么做。如果不是还能用什么?

我最近不得不完成一项类似的任务 (insert,update) 来自数据库的 Active Directory 用户。您可以通过几个选项完成此任务:

  1. 使用第 3 方软件,如他评论中提到的@billinkc。但是,这样做的缺点多于优点。它们中的大多数都不符合成本效益,而且它们对您的操作有限制 (如果您不购买源代码)。 不建议走这条路。

  2. 有一种方法可以使用 SSIS 包来完成此任务。此技术网 article 解释了如何使用外部源将用户导入 Active Directory。 SSIS 包将使用用 VBAC# 编写的外部脚本。文章中提供的 C# 示例是在较旧的 .NET 库中编写的。它使用 DirectoryEntry class。使用这个 class 没有任何问题,但是,对于 .NET 3.5 和更高版本,Microsoft 引入了 System.DirectoryServices.AccountManagement,使用起来更简单。

  3. 使用AccountManagement命名空间自己编写。听起来并不难。

由于您在数据库中拥有所有用户信息,因此您可以编写一个 stored procedure 来 return 用户信息。

您可以从这里开始。

通过调用stored procedure从数据库中加载用户信息。

 public static DataTable GetUserInfoByUsername(string username)
 {
     //call stor proc to return data
     //return datatable or custom class that will hold user iformation
 }

编写两个方法,将 CreateUpdate 一个用户。使用 datatable 中的值填充 Active Directory 中的用户属性。

public static string CreateUser(DataTable dt, string password)
{
   //CREATE CONNECTION TO ACTIVE DIRECTORY
   using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "yourdomain.com"))
   {
       //CREATE A NEW USERPRINCIPAL OBJECT
       using (UserPrincipal principal = new UserPrincipal(ctx))
       {
          //TODO: you need to populate the below info with values from dt
          principal.Enabled = true; //IF NOT ENABLED YOU CAN'T AUTHENTICATE THE USER
          principal.UserPrincipalName = username;
          principal.Name = "name";
          principal.DisplayName = "firstname lastname";
          principal.EmailAddress = "email@test.com";
          principal.VoiceTelephoneNumber = "12345678910";
          principal.GivenName = "firstname";
          principal.Surname = "lastname";
          principal.SetPassword(password);
          try
          {
             principal.Save();
          }
          catch(Exception ex)           
          {
             throw;
          }
       }
    }
 }

您可以采用类似的方法来更新用户。

public static void UpdateUser(string userName, DataTable dt)
{
    using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "yourdomain.com"))
    {
        //you have more than one way to search for a user in AD. Identitype has more choises
        using (UserPrincipal principal = UserPrincipal.FindByIdentity(ctx, Identitype.samAccountName, username))
        {
            if (principal !== null)
            //update user properties with data from datatable dt
            ...
            principal.Save();
        }
    }
}

有件事要记住。默认情况下 UserPrincipal class 不会显示每个活动目录 属性。获取这些属性的最快方法是使用 DirectoryEntry class。您可以调用 principal.

GetUnderlyingObject() 方法
DirectoryEntry de = (principal.GetUnderlyingObject() as DirectoryEntry);

if (de != null)
{
   if (de.Properties.Contains("company"))
   string company = de.Properties["company"][0].ToString();
}

如果你想运行某种工作来执行这些操作,只需让你的stored procedure到return所有用户并在调用任何之前使用foreach循环那些方法。

我建议你编写一个 windows 服务并在那里完成所有这些操作。

*注意 代码不完整。根据您的需要,您需要修改代码。**