Get a Complete Computer List From Active Directory Using .NET 2.0

來源:互聯網
上載者: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=Schofield,DC=com" is the LDAP path for the domain I'm connecting to. This represents an LDAP-based path to the Active Directory domain you are connecting to. You will want to change the code sample to what domain you are connecting to.

Try

Dim enTry As System.DirectoryServices.DirectoryEntry = _

New System.DirectoryServices.DirectoryEntry("LDAP://DC=Steve,DC=Schofield,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 to: " & _

                        resEnt.GetDirectoryEntry().Name.ToString() & _

                        vbCrLf & ex.Message.ToString())

End Try

Next

Catch

End Try

Steve Schofield is a Senior Internet Support Specialist with ORCS 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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.