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 人のユーザー全員の一括更新を行わないでください。管理しやすい単位に分割してください。
まだお読みでない場合は、.NET Framework 3.5でディレクトリ セキュリティ プリンシパルを管理するという MSDN の記事を必ずお読みくださいSystem.DirectoryServices.AccountManagement
。または、System.DirectoryServices.AccountManagement 名前空間に関する MSDN ドキュメントを参照してください。
もちろん、必要に応じて、作成した「例によるクエリ」ユーザー プリンシパルに他のプロパティを指定することもできます。
DisplayName
(通常: 名 + スペース + 姓)
SAM Account Name
- Windows/AD アカウント名
User Principal Name
- "username@yourcompany.com" スタイル名
の任意のプロパティを指定し、UserPrincipal
それらを の「例によるクエリ」として使用できますPrincipalSearcher
。