[ad#ad-336×280]
DirectoryEntryコンポーネントを使用すると、指定したOUにユーザーを作成することができます。
下記に手順を示します。
- DirectoryEntryのPathプロパティにパスを設定します。
- DirectoryEntryのChildrenプロパティが持つFindメソッドを使用してOUを検索し、DirectoryEntry型の変数(仮にouとする)に代入しておきます。
- OUが見つかったら、変数ouのChildrenプロパティのAddメソッドでユーザーを指定し、DirectoryEntry型の変数(仮にuserとする)に代入します。
- 変数userのPropertiesプロパティを使用して”samAccountName”にユーザーを設定します。
- 変数userのCommitChangesメソッドを実行して、ユーザーを作成します。
下記は、指定したOUにユーザーを作成する例です。
VBの例
' [ユーザーの作成]ボタンクリック時の処理 Private Sub btnCreateUser_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreateUser.Click 'LDAPパスを指定 DirectoryEntry1.Path = "LDAP://corp.contoso.com" '★★★指定されたOUの検索★★★ Dim ou As System.DirectoryServices.DirectoryEntry = DirectoryEntry1.Children.Find("OU=" & txtOU.Text) '★★★指定したOUにユーザーを作成する★★★ Dim user As System.DirectoryServices.DirectoryEntry = ou.Children.Add("CN=" & txtUser.Text, "user") user.Properties("samAccountName").Value = txtUser.Text user.CommitChanges() End Sub
C#の例
// [ユーザーの作成]ボタンクリック時の処理 private void btnCreateUser_Click(object sender, EventArgs e) { // LDAPパスを指定 directoryEntry1.Path = "LDAP://corp.contoso.com"; // ★★★指定されたOUの検索★★★ System.DirectoryServices.DirectoryEntry ou = directoryEntry1.Children.Find("OU=" + txtOU.Text); // ★★★指定したOUにユーザーを作成する★★★ System.DirectoryServices.DirectoryEntry user = ou.Children.Add("CN=" + txtUser.Text, "user"); user.Properties["samAccountName"].Value = txtUser.Text; user.CommitChanges(); }
Please follow and like us:
コメント