【发布时间】:2017-01-16 06:34:06
【问题描述】:
我们在 Powershell 中有一个脚本,用于操作 Active Directory。我现在用 C# 编写了它。我的同事说他们必须在 PS 中指定域控制器,否则可能会发生使用 DC A 读取并在 DC B 上写入的情况,这可能会导致问题。
如果我使用 DirectorySearcher 查找条目并对其进行操作,我真的必须指定域控制器吗?或者根据定义,相同的域控制器是否是用于查找对象(DirectorySearcher)并保存它(CommitChanges)的用户?
我不这么认为,因为我只能在搜索部分(DirectorySearcher 的 DirectoryEntry 对象)中指定它,但不能在将它写回 AD 时指定它(CommitChanges)。所以我猜想写的 DC 和读的 DC 一样。
下面我有一个示例,我搜索特定条目并更改属性。
string filter = "(proxyaddresses=SMTP:johndoe@abc.com)";
string searchOU = "ou=Users,dc=abc,dc=com";
DirectoryEntry entry = new DirectoryEntry("LDAP://" + searchOU);
DirectorySearcher search = new DirectorySearcher(entry);
search.Filter = filter;
SearchResult result = search.FindOne();
search.Dispose();
entry.Close();
DirectoryEntry toContact = result.GetDirectoryEntry();
toContact.Properties["showInAddressBook"].Value = addressbook;
toContact.CommitChanges();
toContect.Close();
【问题讨论】:
标签: c# active-directory directoryentry domaincontroller directorysearcher