您可以使用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
的“查询范例”。