我们在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();
答案 0 :(得分:0)
我可能建议使用System.DirectoryServices.AccountManagement命名空间中的可用对象吗?它是.NET的最新成员,可以更加优雅地处理AD工作。它最近给我带来了很多心痛,旧的DirectorySearcher做事的方式部分导致了这一点。让框架承受压力!网上有很多非常有用的文章,例如here。
作为如何使用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();