I want to locate the DNS server, Global catalog server and domain contorller

Posted on 2008-11-12
Last Modified: 2013-12-24
I am looking for some code in either vb or C# to locate the DNS server, the Global Catalog server and the primary domain controller.  Please, don't offer links unless they actually articulate source code.  I am comfortable with AD programming and can find all computers as demonstrated below, but I just want to find servers with specific purposes.
    Public Function GetCatalogServer() As String
        Dim oDE As DirectoryEntry
        Dim oSearch As DirectorySearcher
        Dim oResults As SearchResultCollection

            oDE = GetDirectoryObject("Default")
            oSearch = New DirectorySearcher(oDE)
            oSearch.PageSize = 1000
            oSearch.Filter = "(objectClass=Computer)"
            oSearch.SearchScope = SearchScope.Subtree

            oResults = oSearch.FindAll()

        Catch ex As Exception
            Me.Severity = 3
            Return (Nothing)
        End Try

        Return (Me.m_sServer)
    End Function

Question by:wjolson
    LVL 18

    Accepted Solution

    To identify GC

    strComputer = "atl-dc-01"
    Const NTDSDSA_OPT_IS_GC = 1
    Set objRootDSE = GetObject("LDAP://" & strComputer & "/rootDSE")
    strDsServiceDN = objRootDSE.Get("dsServiceName")
    Set objDsRoot  = GetObject("LDAP://" & strComputer & "/" & strDsServiceDN)
    intOptions = objDsRoot.Get("options")
    If intOptions And NTDSDSA_OPT_IS_GC Then
        WScript.Echo strComputer & " is a global catalog server."
        Wscript.Echo strComputer & " is not a global catalog server."
    End If

    Open in new window

    LVL 18

    Assisted Solution

    To identify Domain controllers:

    Set objDomain = getObject("LDAP://rootDse")
    objDC = objDomain.Get("dnsHostName")
    Wscript.Echo objDC

    Open in new window


    Author Comment


    I presume that in your first example, you are suggesting that I compare a computer in the domain against the constant to determine if it is a DC.  If that is the case, I would have to get all computers in the domain; which I can do.  I was hoping the process was not so brute force.  Hey, any thought on knowing if you have a workgroup server or an actual Domain Controller.

    I will test these 2 options out later today.

    Author Closing Comment

    I still wanted to know about a DC, but no big deal: 2 out of 3 ain't bad.  Besides, the leaves me with something more to learn without being totally spoon fed.  Thanks.
    LVL 18

    Expert Comment

    Unfortunately GC only can be found in a DC. So, knowing the DC status before you check for GC would be easier.

    Also, you may want to take a look at this one--to determine the role of a computer(DC, member sever or workstation):
    strComputer = "."
    Set objWMIService = GetObject("winmgmts:" _
        & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    Set colComputers = objWMIService.ExecQuery _
        ("Select DomainRole from Win32_ComputerSystem")
    For Each objComputer in colComputers
        Select Case objComputer.DomainRole 
            Case 0 
                strComputerRole = "Standalone Workstation"
            Case 1        
                strComputerRole = "Member Workstation"
            Case 2
                strComputerRole = "Standalone Server"
            Case 3
                strComputerRole = "Member Server"
            Case 4
                strComputerRole = "Backup Domain Controller"
            Case 5
                strComputerRole = "Primary Domain Controller"
        End Select
        Wscript.Echo strComputerRole

    Open in new window


    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
    I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
    This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles from a Windows Server 2008 domain controller to a Windows Server 2012 domain controlle…
    This tutorial will walk an individual through the process of configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now