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

  • Thread starter Thread starter ginolard
  • Start date Start date
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
 
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
 
Back
Top