locked
How to get domain RRS feed

  • Question

  • Hello,

     

    I have problems getting domain names. I would like to get all domains in LAN. Because I cannot test the program, can someone tell me, if this is the right way:

     

    _dirEntry.Path = "LDAP://RootDSE"

    _strRootDomain = "LDAP://" & _dirEntry.Properties("rootDomainNamingContext").Value.ToString()

    _dirEntry.Path = _strRootDomain

    _dirSearch.SearchRoot = _dirEntry

    _dirSearch.Filter = "(objectClass=domainDNS)"

    _resultColl = _dirSearch.FindAll

    For Each _result As SearchResult In _resultColl

    cmbDomains.Items.Add(_result.Properties("name").Item(0).ToString)

    Next

     

    Problem No. 2 is that I have domain "comp-domain" (LDAP://DC=comp-domain,DC=com). But when logging into domain I use only: comp\pero. If I use Environment.userdomainname then I receive correct value: "comp". But how can I get domain even if I am not logged on domain or if I am in any other domain?? Is any other way to get all domains with userdomain names? Thanks.

    Tuesday, September 11, 2007 11:34 AM

Answers

  •  

    Thanks for you help, but I found better solution for me:

     

    Dim objSearcher As New ManagementObjectSearcher("SELECT * FROM Win32_NTDomain")

    Dim objDomain As ManagementObject

    For Each objDomain In objSearcher.Get()

    MsgBox("Domain = " & objDomain("Name").ToString())

    Next objDomain

    this returns me exactly what I want. BIOS names of domains. Then I select groups from Win32_Group where domain = ....

    Thursday, September 13, 2007 9:22 AM

All replies

  • _Pero_,

     

    According to your question on getting the domain name in VB.NET, I would like to provide you the following suggestions on the issue:

     

    1. Please Add the reference to System.DirectoryServices, replace NetworkName with your current network name. This will only work if you are connected to an active directory.

     

            Dim de As New

            System.DirectoryServices.DirectoryEntry("LDAP://NetworkName")

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

            Dim r As System.DirectoryServices.SearchResult

            ds.Filter = "(objectClass=computer)"

            Try

                For Each r In ds.FindAll

                    Dim s As String

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

                Next

            Catch e As Exception

                Console.WriteLine(e.ToString)

            End Try

     

    For further information with the full code sample, please take a look at the following thread. There is a sample on the issue

     

    This example encapsulates the API call for you in the MasterBrowserList......

    Thursday, September 13, 2007 9:18 AM
  • 2. This VB function returns the DNS domain name (westcoast.mycorp.com) from the passed LDAP distinguishedName (CN=Administrator,DN=Users,DC=westcoast,DC=mycorp,DC=com). The function is from this article.

     

        Function DN2DomainName(ByVal strDN As String)

            On Error Resume Next

            Dim strDomainName As String

            Dim strDNParts() As String

            Dim intI As Integer

            strDomainName = ""

            strDNParts = Split(strDN, ",")

            ReDim Preserve strDNParts(UBound(strDNParts))

            ' Work backwords through DN to assemble domain name

            For intI = UBound(strDNParts) To 0 Step -1

                If (InStr(UCase(strDNParts(intI)), "DC=")) Then

                    strDomainName = "." & Replace(strDNParts(intI), "DC=", "") & strDomainName

                Else

                    Exit For ' Short circuit when we run out of "DC=" parts

                End If

            Next

            ' Chop off leading '.'

            strDomainName = Right$(strDomainName, Len(strDomainName) - 1)

            DN2DomainName = strDomainName

        End Function

     

    Hope that can provide you some idea.

     

    Thursday, September 13, 2007 9:19 AM
  •  

    Thanks for you help, but I found better solution for me:

     

    Dim objSearcher As New ManagementObjectSearcher("SELECT * FROM Win32_NTDomain")

    Dim objDomain As ManagementObject

    For Each objDomain In objSearcher.Get()

    MsgBox("Domain = " & objDomain("Name").ToString())

    Next objDomain

    this returns me exactly what I want. BIOS names of domains. Then I select groups from Win32_Group where domain = ....

    Thursday, September 13, 2007 9:22 AM
  • _Pero_,

     

    Thanks very much for sharing your solution. I believe your post can help many people on this kind of problems.

     

    Thanks again for your question and feedback.

     

    Thursday, September 13, 2007 9:27 AM