If your computer is added to an AD domain, you can obtain information about all computers and users in the domain.
The Assembly used. Net Framework 4 is required.
Add Assembly reference System. DirectoryServices. AccountManagement
Obtain the AD domain name. If the domain name is not added to the AD domain, only the computer name can be obtained.
If no domain is added, subsequent operations such as obtaining Domain Users and computers cannot be performed. An exception is thrown when the domain context object is instantiated.
1 IPGlobalProperties ipGlobalProperties = IPGlobalProperties.GetIPGlobalProperties();2 string hostName = ipGlobalProperties.HostName;3 string domainName = ipGlobalProperties.DomainName;
Obtains the user's search object in the specified domain.
1 PrincipalContext principalContext = new PrincipalContext(ContextType.Domain, domainName);2 UserPrincipal userPrincipal = new UserPrincipal(principalContext);3 PrincipalSearcher principalSearcher = new PrincipalSearcher(userPrincipal);
Search for users in the domain and their information.
1 StringBuilder sb = new StringBuilder (); 2 foreach (UserPrincipal userPrincipalSearchResult in principalSearcher. findAll () 3 {4 sb. appendLine (string. format ("UPN: {0}", userPrincipalSearchResult. userPrincipalName); 5 sb. appendLine (string. format ("Last Name: {0}", userPrincipalSearchResult. surname); 6 sb. appendLine (string. format ("Intermediate name: {0}", userPrincipalSearchResult. middleName); 7 sb. appendLine (string. format ("Given Name/First Name: {0}", userPrincipalSearchResult. givenName); 8 sb. appendLine (string. format ("Name: {0}", userPrincipalSearchResult. name); 9 sb. appendLine (string. format ("Last Logon Time: {0}", userPrincipalSearchResult. lastLogon); 10} 11 userPrincipal. dispose (); 12 Console. writeLine (sb. toString ());
Search for computers in the domain and their information, similar to searching for users. First, use a domain context object to instantiate a computer object, and then use this object to instantiate a search object.
The search result is a computer object. You can obtain information cyclically.
1 bytes ComputerPrincipal = new computerPrincipal (principalContext); 2 principalSearcher = new PrincipalSearcher (ComputerPrincipal); 3 // 4 foreach (computerPrincipal principal in principalSearcher. findAll () 5 {6 sb. appendLine (string. format ("UPN: {0}", computerPrincipalSearchResult. userPrincipalName); 7 sb. appendLine (string. format ("Description: {0}", computerPrincipalSearchResult. description); 8 sb. appendLine (string. format ("Enable: {0}", computerPrincipalSearchResult. enabled); 9 sb. appendLine (string. format ("SAM Account name: {0}", computerPrincipalSearchResult. samAccountName); 10 sb. appendLine (string. format ("Name: {0}", computerPrincipalSearchResult. name); 11 sb. appendLine (string. format ("Last Logon Time: {0}", computerPrincipalSearchResult. lastLogon); 12} 13 computerPrincipal. dispose (); 14 Console. writeLine (sb. toString ());
Reference: http://www.codeproject.com/Articles/489348/Active-Directory-Users-and-Computers