Get a complete computer list from active directory using. NET 2.0

Source: Internet
Author: User

 

This code sample shows how to retrieve a list of computers from Active Directory and use a for/next to loop through the collection. I 've used this code sample on several scripts and using active directory as the authoritative source for a list of machines is handy. the process running this code sample normally requires being a domain administrator level to retrieve this information.

Note: The "LDAP: // dc = Steve, Dc = scholar field, Dc = com" is the LDAP path for the domain I'm connecting. this represents an LDAP-based path to the Active Directory domain you are connecting. you will want to change the code sample to what domain you are connecting.

Try

Dim entry as system. directoryservices. directoryentry = _

New system. directoryservices. directoryentry ("LDAP: // dc = Steve, Dc = scholar field, Dc = com ")

Dim mysearcher as system. directoryservices. directorysearcher = _

New system. directoryservices. directorysearcher (entry)

Mysearcher. Filter = ("(objectclass = computer )")

Dim resent as system. directoryservices. searchresult

For each resent in mysearcher. findall ()

Try

'The reason for using the mid statement is when retrieving computers

'With this method, they are formatted Cn = computername. The Mid statement

'Parses out just the computername. 'cn 'stands for 'common name', this is

'The way LDAP stores the computer variable.

Console. writeline (": Processing :"&_

Mid (resent. getdirectoryentry (). Name. tostring (), 4 ))

Catch ex as exception

Console. writeline ("trying to connect :"&_

Resent. getdirectoryentry (). Name. tostring ()&_

Vbcrlf & Ex. Message. tostring ())

End try

Next

Catch

End try

Steve Schofield is a senior Internet support specialistOrcs web, Inc.-A company that provides Managed complex hosting for clients who develop and deploy their applications on Microsoft Windows platforms.

Dotnetbips.com: blossom your. Net skills

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.