2013-02-15 80 views
2

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

在我的Windows服务器中,大约有1500个用户,Active Directory中的用户信息不正确,需要更新。电子邮件字段应该更新,例如,当前的电子邮件是[email protected],我想将其更改为"user name" + email.com

例如:

  1. [email protected] ==>[email protected];
  2. [email protected] ==>[email protected];
  3. [email protected] ==>[email protected]

可能有人能帮助提供意见?先谢谢你。

回答

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 - 你的 “[email protected]” 样式名

可以SPE将UserPrincipal上的任何属性都作为属性,并将它们用作您的PrincipalSearcher的“查询范例”。