我已经寻找了相当长的时间,希望使用C#代码查询Active Directory用户的所有属性,无论它们是否具有NULL值。这些属性可以通过域服务器上ADSI Edit中用户的属性的属性编辑器选项卡中查看。
我需要动态检索这些属性,这意味着我可能无法通过MSDN上的ADSI文档可靠地获得这些属性名称,并且因为并非所有这些属性都可能是用户对象特定的:
https://msdn.microsoft.com/en-us/library/ms675090(v=vs.85).aspx以下是我迄今为止尝试过的内容,但只获得了用户对象的一小部分属性:
PS command
Get-ADUser -Identity administrator -Properties
: This retrieved a good part of the attributes, but not nearly all of them and I do not know what .NET Classes and methods are invoked during this command, sinceTypeName = Microsoft.ActiveDirectory.Management.ADUser
, which does not exist in the .NET framework. How can I see the specific methods that are using from .NET in PS?C# calling this method:
public bool GetUserAttributes(out List<string> userAttributes, string userName) { userAttributes = new List<string>(); var valueReturn = false; try { const string pathNameDomain = "LDAP://test.local"; var directoryEntry = new DirectoryEntry(pathNameDomain); var directorySearcher = new DirectorySearcher(directoryEntry) { Filter = "(&(objectClass=user)(sAMAccountName=" + userName + "))" }; var searchResults = directorySearcher.FindAll(); valueReturn = searchResults.Count > 0; StreamWriter writer = new StreamWriter("C:\\LDAPGETUSERADEXAMPLE.txt"); foreach (SearchResult searchResult in searchResults) { foreach (var valueCollection in searchResult.Properties.PropertyNames) { userAttributes.Add(valueCollection.ToString() + " = " + searchResult.Properties[valueCollection.ToString()][0].ToString()); try { writer.WriteLine("Bruger attribut:" + valueCollection); } catch (Exception) { throw; } } }
C# calling this method:
public List<string> GetADUserAttributes() { string objectDn = "CN=testuser,OU=TEST,DC=test,DC=local"; DirectoryEntry objRootDSE = new DirectoryEntry("LDAP://" + objectDn); List<string> attributes = new List<string>(); foreach (string attribute in objRootDSE.Properties.PropertyNames) { attributes.Add(attribute); } return attributes; }
我知道默认情况下,Active Directory只显示默认属性或具有值的属性,我正在尝试克服这个限制。
编辑1:
我暂时搁置了具体问题。我一直在尝试基准测试以下哪种方法最快地检索(读取操作)10,000个名为“testuser”的单个AD用户的SAM帐户名,我进行基准测试的方法如下:
1.完成时间:约500毫秒:ADSI-system.directoryservices 2.完成时间:约2700毫秒:Principal-searcher system.directoryservices.accountmanagement 3.完成时间:约无法工作:LDAP-System.DirectoryServices.Protocols 4.完成时间:约60毫秒:SQL-System.Data.SqlClient
我正在从Windows 10机器的工作站查询用户信息,该工作站位于我正在查询的域中。工作站(4个vcpu),DC(2个vpu)和DB(2个vcpu)服务器作为Hyper V vm运行。