c# DirectoryEntry InvokeSet HomeDirectory 和 HomeDrive,错误
我正在尝试修改指定 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也没有理由调用 InvokeSet。这是执行此操作的正确方法:
There's no reason to call InvokeSet, also. This is the correct way to do this:
看起来您正在使用
Entry
,它指向您的目录根,而不是您找到的对象,这就是调用失败的原因。我相信你可以将 foreach 循环更改为:
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: