Windows 更新广告中的用户信息

Windows 更新广告中的用户信息,windows,active-directory,Windows,Active Directory,我之前发布过一个问题,但可能是我没有清楚地描述我的问题,因此我重新改写了我的问题,希望大家都能理解 在我的Windows服务器中,大约有1500个用户,Active Directory中的用户信息不正确,需要更新。应更新电子邮件字段,例如,当前电子邮件为tom。chan@email.com,我想将其更改为“用户名”+email.com 例如: 汤姆。chan@email.com==>user1@email.com 艾米。yuen@email.com==>user2@email.com 杰克。hu

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

在我的Windows服务器中,大约有1500个用户,Active Directory中的用户信息不正确,需要更新。应更新电子邮件字段,例如,当前电子邮件为
tom。chan@email.com
,我想将其更改为
“用户名”+email.com

例如:

  • 汤姆。chan@email.com==>
    user1@email.com
  • 艾米。yuen@email.com==>
    user2@email.com
  • 杰克。hung@email.com==>
    user3@email.com

  • 有人能帮忙提建议吗?提前感谢。

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

    // 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文章,这篇文章很好地展示了如何最好地利用
    System.DirectoryServices.AccountManagement
    中的新功能。或者查看名称空间

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

    • DisplayName
      (通常为:名字+空格+姓氏)
    • SAM帐户名
      -您的Windows/AD帐户名
    • 用户主体名称
      -您的”username@yourcompany.com“样式名
    您可以在
    UserPrincipal
    上指定任何属性,并将其用作
    PrincipalSearcher
    的“示例查询”