[英]Add enabled Computer to Active Directory OU
我正在嘗試以編程方式將計算機添加到我公司的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 - >服務器無法完成請求。
要么
newComputer.Properties["Enabled"].Value = true;
但是我得到一個DirectoryServicesCOMException - >請求的操作不滿足至少一個針對此對象類條件的約束。
請注意,例外情況從德語翻譯成英語!
謝謝你的幫助!
我認為有兩件事可能是錯的,但是我做了這樣的事情已經有很長時間了,所以我可能錯了......
首先,你什么時候設置userAccountControl
標志? 我似乎記得你應該在新條目的CommitChanges
之后執行此操作。 像這樣:
DirectoryEntry newComputer =
dirEntry.Children.Add("CN=" + ComputerName, "computer");
newComputer.CommitChanges();
newComputer.Properties["userAccountControl"].Value = 0x200;
newComputer.CommitChanges();
其次,您可以嘗試設置UF_WORKSTATION_TRUST_ACCOUNT
標志( 0x1000
)而不是UF_NORMAL_ACCOUNT
( 0x200
)。
您還可以檢查條目的sAMAccountType
是否為SAM_MACHINE_ACCOUNT
( 0x30000001
)。 我認為這應該是自動的,但檢查不會有害。
差不多一年后,還有一年更聰明,我確切地知道我做錯了什么。
所以我想和你分享正確的方法,即使我已經選擇了答案。
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();
sAMAccountName賦
在這里找到解釋
計算機對象的sAMAccountName屬性是附加了尾隨美元符號“$”的計算機的NetBIOS名稱。 除了將對象標記為計算機(具有類用戶)之外,它還有助於確保唯一性。 sAMAccountName值在域中必須是唯一的。 請注意,計算機對象的公用名(cn屬性的值)沒有尾隨的“$”,但cn也不能唯一地標識AD中的對象。 Common Name只需要在OU或容器中是唯一的。
機器帳戶在其sAMAccountName屬性中始終被賦予尾隨美元符號“$”; 這導致它們不被某些API枚舉,因此不會顯示在某些用戶界面中,而這些用戶界面中只能看到“用戶”帳戶。
的UserAccountControl
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.