【问题标题】:Update user info in AD [closed]在 AD 中更新用户信息 [关闭]
【发布时间】:2013-02-15 01:51:33
【问题描述】:

我之前发过一个问题,但可能是我的问题描述的不是很清楚,所以我重新写了我的问题,希望大家能理解。

在我的 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

谁能帮忙给点建议?提前谢谢你。

【问题讨论】:

    标签: windows active-directory


    【解决方案1】:

    您可以使用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 文章 Managing Directory Security Principals in the .NET Framework 3.5,它很好地展示了如何充分利用 System.DirectoryServices.AccountManagement 中的新功能。或查看MSDN documentation on the System.DirectoryServices.AccountManagement 命名空间。

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

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

    您可以在UserPrincipal 上指定任何属性并将其用作PrincipalSearcher 的“示例查询”。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-15
      • 1970-01-01
      • 1970-01-01
      • 2018-04-16
      • 1970-01-01
      • 1970-01-01
      • 2013-08-26
      • 2016-10-03
      相关资源
      最近更新 更多