• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 432
  • Last Modified:

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

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

  • 3
  • 2
2 Solutions
To identify GC

strComputer = "atl-dc-01"
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

To identify Domain controllers:

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

Open in new window

wjolsonAuthor Commented:

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.
wjolsonAuthor Commented:
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.
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

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Improve Your Query Performance Tuning

In this FREE six-day email course, you'll learn from Janis Griffin, Database Performance Evangelist. She'll teach 12 steps that you can use to optimize your queries as much as possible and see measurable results in your work. Get started today!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now