在公元更新用户信息 [英] Update user info in AD

查看:144
本文介绍了在公元更新用户信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经发布了一个问题之前,但可能是我没有描述清楚我的问题,所以我重新改写我的问题,希望大家可以理解。

I have posted a question before, but may be I am not clearly described my problem, therefore I re-rewrite my question, hoping everyone may understand it.

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

In my Windows server, there are around 1500 users, the user info in Active Directory is not correct and needs to be updated. The e-mail field should be updated, for example, the current email is tom.chan@email.com, I want to change it to "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
  1. tom.chan@email.com ==> user1@email.com ;
  2. amy.yuen@email.com ==> user2@email.com ;
  3. jacky.hung@email.com ==> user3@email.com

可能有人能帮助提供意见?谢谢你在前进。

Could anyone can help to give advice? Thank you in advance.

推荐答案

您可以使用 PrincipalSearcher 和查询通过例如主要做你的搜索:

You can use a PrincipalSearcher and a "query-by-example" principal to do your searching:

// 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();
           }
       }
    }
}

使用这种方法,你可以遍历用户和更新它们 - 一次:我不是很确定我知道你想为你的的电子邮件地址,使用什么样的... ..所以也许你需要适应这个您的需求。

Using this approach, you could loop over your users and update them all - again: I'm not entirely sure I understand what you want to use as your new e-mail address..... so maybe you need to adapt this to your needs.

另外:我会建议的不可以这样做是为了您的整个用户群的一次!运行它的群体,如由欧,或按姓氏或某事的首字母 - 不要做所有的1500个用户的大规模更新一次 - 将它分解为可管理的部分。

Also: I would recommend not doing this to your entire user base at once! Run it in groups, e.g. by OU, or by initial letter of last name or something - don't do a mass update of all 1500 users at once - break it down into manageable pieces.

如果您还没有 - 绝对阅读MSDN文章管理目录安全主体在.NET Framework 3.5 这很好地说明如何使新功能的最佳使用 System.DirectoryServices.AccountManagement 。还是看在System.DirectoryServices.AccountManagement MSDN文档>命名空间。

If you haven't already - absolutely read the MSDN article Managing Directory Security Principals in the .NET Framework 3.5 which shows nicely how to make the best use of the new features in System.DirectoryServices.AccountManagement. Or see the MSDN documentation on the System.DirectoryServices.AccountManagement namespace.

当然,这取决于你的需要,你可能想在你创建的查询通过例如用户主体指定其他属性:

Of course, depending on your need, you might want to specify other properties on that "query-by-example" user principal you create:

  • 显示名称(通常是:第一个名称+空格+姓)
  • SAM帐户名 - 您的Windows / AD帐户名
  • 用户主要名称 - 你的username@yourcompany.com样式名
  • DisplayName (typically: first name + space + last name)
  • SAM Account Name - your Windows/AD account name
  • User Principal Name - your "username@yourcompany.com" style name

您可以指定任何关于 UserPrincipal 属性,并使用这些作为查询通过例如为您 PrincipalSearcher

You can specify any of the properties on the UserPrincipal and use those as "query-by-example" for your PrincipalSearcher.

这篇关于在公元更新用户信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆