Here is a little VB.NET function I’ve written that will provide you with the names of all of the domain controllers in your Active Directory domain – note that you need to add a reference to System.DirectoryServices first.
Private Function GetDomainControllers() As List(Of String)
Dim DcList As New List(Of String)
Dim DcList As New List(Of String)
Using Searcher As New DirectorySearcher(New DirectoryEntry)
Searcher.Filter = "(primaryGroupID=516)"
For Each DC As SearchResult In Searcher.FindAll
DcList.Add(DC.GetDirectoryEntry.Name.Remove(0, 3))
Next
End Using
Return DcList
End Function
EDIT: I just found that there is a much simpler way of doing this! You simply loop through the DomainControllers collection like so:
For Each DC As DirectoryServices.ActiveDirectory.DomainController In DirectoryServices.ActiveDirectory.Domain.GetCurrentDomain.DomainControllers
'You can get all sorts of useful info from the DomainController object
'but here is a simple example of getting the name and IP
MessageBox.Show(DC.Name & " – " & DC.IPAddress)
Next
'You can get all sorts of useful info from the DomainController object
'but here is a simple example of getting the name and IP
MessageBox.Show(DC.Name & " – " & DC.IPAddress)
Next
Hope it is of some use to someone in the future.
Chris
AWSOMMME thx chris this is just what i needed 😉 me happy
thx chris. do you have a similar function to show all domains in the forest? i am using the below code however it is terribly slow.. is there a better way to do this?
Dim domains As DomainCollection = Forest.GetCurrentForest.Domains
For Each domain As Domain In domains
MsgBox(domain.Name)
Next
Hi Peter,
That’s how I would do it, if it is slow then I would guess it is slow links or DNS problems that are causing the speed issue.