C # obtain computer and user information in the AD domain

Source: Internet
Author: User

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

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.