0

我们在 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();
4

1 回答 1

0

我可以推荐使用 System.DirectoryServices.AccountManagement 命名空间中可用的对象吗?它是 .NET 的最新成员,可以更优雅地处理 AD 工作。它最近为我节省了很多心痛,这是旧的 DirectorySearcher 做事方式的部分原因。让框架承受压力!网上有很多非常有用的文章,比如这里

作为如何使用 PrincipalContext 搜索 Active Directory 的示例,试试这个:

var adContext = new PrincipalContext(ContextType.Domain);
var queryTemplateUser = new UserPrincipal(adContext);
queryTemplateUser.SamAccountName = "HenryCrunn";
var ldapSearcher = new PrincipalSearcher(queryTemplateUser);
var searchResults = ldapSearcher.FindAll();
于 2016-09-08T12:04:37.520 回答