VB.NET newbie - Get list of domains in a forest

G

ginolard

I'm trying to convert some code from VBscript to VB.Net and am getting
stuck on how it works with Active Directory.

I need to get a list of domains in the current forest. What's wrong
with my code?

Dim objRootLDAP
objRootLDAP = GetObject("LDAP://RootDSE")

Dim de As New
System.DirectoryServices.DirectoryEntry("LDAP://CN=Partitions," +
objRootLDAP.Get("ConfigurationNamingContext"))

Dim ds As New System.DirectoryServices.DirectorySearcher(de)

Dim r As System.DirectoryServices.SearchResult

ds.Filter = "(&(objectcategory=crossRef)(systemFlags=3))"
ds.SearchScope = SearchScope.OneLevel
ds.PropertiesToLoad.Add("nCName")


For Each r In ds.FindAll

Console.WriteLine(r.GetDirectoryEntry.Name.ToString)

Next
 
G

ginolard

I figured it out myself, I must be learning something!

objRootLDAP = GetObject("LDAP://RootDSE")


Dim deEntry As New DirectoryEntry("LDAP://CN=Partitions, " &
objRootLDAP.Get("ConfigurationNamingContext").ToString())

Dim DSearch As New DirectorySearcher(deEntry)
DSearch.Filter = "(&(objectcategory=crossRef)(systemFlags=3))"
DSearch.PropertiesToLoad.Add("cn")
DSearch.Sort.PropertyName = "cn"

Dim sResultSet As SearchResult

For Each sResultSet In DSearch.FindAll()

Console.Write(GetProperty(sResultSet, "cn"))

Next

Public Function GetProperty(ByVal srSearchResult As SearchResult,
ByVal strPropertyName As String) As String
If srSearchResult.Properties.Contains(strPropertyName) Then
Return
srSearchResult.Properties(strPropertyName)(0).ToString()
Else
Return String.Empty
End If
End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top