c# DirectoryEntry InvokeSet HomeDirectory 和 HomeDrive,错误

发布于 2024-12-06 12:03:53 字数 875 浏览 0 评论 0原文

我正在尝试修改指定 OU 中每个 AD 用户的配置文件/主目录/主驱动器设置,

下面有一些非常基本的代码应该实现此功能,但会抛出以下异常:

请求的操作不满足一个或多个约束 与对象的类相关联。

有人遇到过这个问题吗?如果有的话,有办法解决吗?

谢谢。

DirectoryEntry Entry = new DirectoryEntry("LDAP://OU=Company,DC=corp,DC=Placeholder,DC=com", null, null, AuthenticationTypes.Secure);

DirectorySearcher Searcher = new DirectorySearcher(Entry);
Searcher.SearchScope = SearchScope.Subtree;

Searcher.PropertiesToLoad.Add("sAMAccountName");
Searcher.Filter = "(&(objectClass=user)(objectCategory=person))";

foreach (SearchResult AdObj in Searcher.FindAll())
{
   Entry.InvokeSet("HomeDirectory", @"\\winfileserver\" + Convert.ToString(AdObj.Properties["sAMAccountName"][0]));
   Entry.InvokeSet("HomeDrive", "H");
   Entry.CommitChanges();
}
catch (Exception ex)
{
   richTextBox1.Text += ex.Message;
}

I'm trying to modify the Profiles / Home Directory / Home Drive setting for each AD user in a specified OU,

I have below some very basic code that should achieve this feat but is instead throwing the following exception:

The requested operation did not satisfy one or more constraints
associated with the class of the object.

Has anyone had this problem and if so, have a way of fixing it?

Thank you.

DirectoryEntry Entry = new DirectoryEntry("LDAP://OU=Company,DC=corp,DC=Placeholder,DC=com", null, null, AuthenticationTypes.Secure);

DirectorySearcher Searcher = new DirectorySearcher(Entry);
Searcher.SearchScope = SearchScope.Subtree;

Searcher.PropertiesToLoad.Add("sAMAccountName");
Searcher.Filter = "(&(objectClass=user)(objectCategory=person))";

foreach (SearchResult AdObj in Searcher.FindAll())
{
   Entry.InvokeSet("HomeDirectory", @"\\winfileserver\" + Convert.ToString(AdObj.Properties["sAMAccountName"][0]));
   Entry.InvokeSet("HomeDrive", "H");
   Entry.CommitChanges();
}
catch (Exception ex)
{
   richTextBox1.Text += ex.Message;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

醉城メ夜风 2024-12-13 12:03:53

也没有理由调用 InvokeSet。这是执行此操作的正确方法:

foreach (SearchResult AdObj in Searcher.FindAll()) { 
  DirectoryEntry user = AdObj.GetDirectoryEntry(); 
  user.Properties["HomeDirectory"].Value = @"\\winfileserver\" + Convert.ToString(AdObj.Properties["sAMAccountName"][0]); 
  user.Properties["HomeDrive"].Value = "H"; 
  user.CommitChanges(); 
} 

There's no reason to call InvokeSet, also. This is the correct way to do this:

foreach (SearchResult AdObj in Searcher.FindAll()) { 
  DirectoryEntry user = AdObj.GetDirectoryEntry(); 
  user.Properties["HomeDirectory"].Value = @"\\winfileserver\" + Convert.ToString(AdObj.Properties["sAMAccountName"][0]); 
  user.Properties["HomeDrive"].Value = "H"; 
  user.CommitChanges(); 
} 
寻找一个思念的角度 2024-12-13 12:03:53

看起来您正在使用 Entry ,它指向您的目录根,而不是您找到的对象,这就是调用失败的原因。

我相信你可以将 foreach 循环更改为:

foreach (SearchResult AdObj in Searcher.FindAll()) {
  DirectoryEntry user = AdObj.GetDirectoryEntry();
  user.InvokeSet("HomeDirectory", @"\\winfileserver\" + Convert.ToString(AdObj.Properties["sAMAccountName"][0]));
  user.InvokeSet("HomeDrive", "H");
  user.CommitChanges();
}

It looks like you're using Entry which is pointing to your directory root, not the object you found and that's why the call is failing.

I believe you can change your foreach loop to:

foreach (SearchResult AdObj in Searcher.FindAll()) {
  DirectoryEntry user = AdObj.GetDirectoryEntry();
  user.InvokeSet("HomeDirectory", @"\\winfileserver\" + Convert.ToString(AdObj.Properties["sAMAccountName"][0]));
  user.InvokeSet("HomeDrive", "H");
  user.CommitChanges();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文