2

我之前发布过一个问题,但可能是我没有清楚地描述我的问题,因此我重新编写了我的问题,希望大家能理解它。

在我的 Windows 服务器中,大约有 1500 个用户,Active Directory 中的用户信息不正确,需要更新。邮箱字段要更新,比如当前邮箱是tom.chan@email.com,我想改成"user name" + email.com

例如:

  1. tom.chan@email.com==> user1@email.com;
  2. amy.yuen@email.com==> user2@email.com;
  3. jacky.hung@email.com==>user3@email.com

任何人都可以帮忙提供建议吗?先感谢您。

4

1 回答 1

1

您可以使用 aPrincipalSearcher和“按示例查询”主体进行搜索:

// create your domain context
using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain))
{
    // define a "query-by-example" principal - here, we search for a UserPrincipal 
    // with last name (Surname) that starts with "A"
    UserPrincipal qbeUser = new UserPrincipal(ctx);
    qbeUser.Surname = "A*";

    // create your principal searcher passing in the QBE principal    
    using (PrincipalSearcher srch = new PrincipalSearcher(qbeUser))
    {
       // find all matches
       foreach(var found in srch.FindAll())
       {
           // now here you need to do the update - I'm not sure exactly *WHICH*
           // attribute you mean by "username" - just debug into this code and see
           // for yourself which AD attribute you want to use
           UserPrincipal foundUser = found as UserPrincipal;

           if(foundUser != null)
           {
              string newEmail = foundUser.SamAccountName + "@email.com";
              foundUser.EmailAddress = newEmail;
              foundUser.Save();
           }
       }
    }
}

使用这种方法,您可以遍历您的用户并更新它们 - 再次:我不完全确定我理解您想要用作电子邮件地址的内容.....所以也许您需要调整它以适应您的需求。

另外:我建议不要一次对您的整个用户群执行此操作!以组的形式运行它,例如按 OU,或按姓氏的首字母或其他方式 - 不要一次对所有 1500 个用户进行大规模更新 - 将其分解为可管理的部分。

如果您还没有 - 一定要阅读 MSDN 文章Managing Directory Security Principals in the .NET Framework 3.5,它很好地展示了如何充分利用 .NET Framework 中的新功能System.DirectoryServices.AccountManagement。或查看System.DirectoryServices.AccountManagement命名空间上的 MSDN 文档。

当然,根据您的需要,您可能希望在您创建的“示例查询”用户主体上指定其他属性:

  • DisplayName(通常:名字 + 空格 + 姓氏)
  • SAM Account Name- 您的 Windows/AD 帐户名称
  • User Principal Name- 您的“username@yourcompany.com”样式名称

您可以在 上指定任何属性UserPrincipal并将其用作PrincipalSearcher.

于 2013-02-15T05:58:35.477 回答