2

以前に質問を投稿しましたが、問題が明確に説明されていない可能性があるため、質問を書き直して、誰もが理解できるようにします。

私の 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

誰でもアドバイスを手伝ってもらえますか?前もって感謝します。

4

1 に答える 1

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 人のユーザー全員の一括更新を行わないでください。管理しやすい単位に分割してください。

まだお読みでない場合は、.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

于 2013-02-15T05:58:35.477 に答える