Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将启用的计算机添加到Active Directory OU_C#_Active Directory_Windows Server 2003 - Fatal编程技术网

C# 将启用的计算机添加到Active Directory OU

C# 将启用的计算机添加到Active Directory OU,c#,active-directory,windows-server-2003,C#,Active Directory,Windows Server 2003,我正在尝试以编程方式将计算机添加到公司的Active Directory中。 我在互联网上搜索了很长时间,但我找不到解决办法 我的代码: DirectoryEntry dirEntry = new DirectoryEntry("LDAP://OU=ou2example,OU=ou1example,DC=site,DC=company,DC=com"); dirEntry.Username = "username"; dirEntry.Password = "password"; Directo

我正在尝试以编程方式将计算机添加到公司的Active Directory中。
我在互联网上搜索了很长时间,但我找不到解决办法

我的代码:

DirectoryEntry dirEntry = new DirectoryEntry("LDAP://OU=ou2example,OU=ou1example,DC=site,DC=company,DC=com");
dirEntry.Username = "username";
dirEntry.Password = "password";
DirectoryEntry newComputer = dirEntry.Children.Add("CN=" + ComputerName, "computer");
newComputer.CommitChanges();
我的问题是:

计算机已添加到Active Directory。但它被标记为已禁用

我尝试执行以下操作以启用计算机:

newComputer.Properties["userAccountControl"].Value = 0x200;
但是我得到一个DirectoryServicesCOMException-->服务器无法完成请求

但我得到一个DirectoryServicesCOMException-->请求的操作至少不满足此对象类条件的一个约束

请注意,例外情况是从德语翻译成英语


谢谢你的帮助

我认为有两件事可能是错的,但我已经很久没有做过这样的事了,所以我可能错了

首先,什么时候设置
userAccountControl
标志?我似乎记得您应该在新条目的
提交更改后执行此操作。所以像这样:

DirectoryEntry newComputer =
    dirEntry.Children.Add("CN=" + ComputerName, "computer");
newComputer.CommitChanges();
newComputer.Properties["userAccountControl"].Value = 0x200;
newComputer.CommitChanges();
其次,您可以尝试设置标志(
0x1000
)而不是(
0x200


您还可以检查条目的
sAMAccountType
是否为
SAM\u MACHINE\u ACCOUNT
0x3000001
)。我认为这应该是自动的,但检查起来没有什么坏处。

将近一年后,也就是一年前,我知道我到底做错了什么

所以我想与大家分享正确的方法,尽管我已经选择了一个答案

代码 解释 sAMAccountName

发现的解释

计算机对象的sAMAccountName属性是计算机的NetBIOS名称,后面附加了一个美元符号“$”。除了将对象标记为计算机(具有类user)之外,它还有助于确保唯一性。sAMAccountName值在域中必须是唯一的。请注意,计算机对象的公用名称(cn属性的值)没有尾随“$”,但cn也不能唯一标识AD中的对象。公用名称只需在OU或容器中唯一

机器帐户在其sAMAccountName属性中始终有一个尾随美元符号“$”;这导致它们不会被某些API枚举,因此不会显示在某些用户界面中,在这些用户界面中,人们只能看到“用户”帐户

UserAccountControl


在提交更改之前,我正在设置userAccountControl标志。当我随后设置它们时,以及当我使用0x1000时,我没有得到异常,但计算机仍处于禁用状态:(不要紧!它现在正在工作……先生,您是个天才!对于新创建的对象,UserAccountControl的值有一定的限制。我在使用空密码创建新用户帐户时遇到了类似问题。例如,如果“不需要密码”,服务器拒绝创建用户)未设置标志且未提供密码。我认为您遇到了相同的问题。请尝试手动创建计算机帐户,检查该值,并使用0x200对该值执行二进制或操作,以确保您没有执行任何潜在危险的操作。
DirectoryEntry newComputer =
    dirEntry.Children.Add("CN=" + ComputerName, "computer");
newComputer.CommitChanges();
newComputer.Properties["userAccountControl"].Value = 0x200;
newComputer.CommitChanges();
DirectoryEntry dirEntry = new DirectoryEntry(“LDAP Path”);
DirectoryEntry newComputer = dirEntry.Children.Add(“CN=Hostname”, “computer”);
newComputer.Properties[“sAMAccountName”].Value = Hostname + “$”;
newComputer.Properties[“UserAccountControl”].Value = 0x1020;
newComputer.CommitChanges();