[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


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

Posted on 2008-11-12
Medium Priority
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
  • 3
  • 2
LVL 18

Accepted Solution

Americom earned 2000 total points
ID: 22947452
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

LVL 18

Assisted Solution

Americom earned 2000 total points
ID: 22947456
To identify Domain controllers:

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

Open in new window


Author Comment

ID: 22949502

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

ID: 31516217
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

ID: 22952558
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

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In today's business world, data is more important than ever for informing marketing campaigns. Accessing and using data, however, may not come naturally to some creative marketing professionals. Here are four tips for adapting to wield data for insi…
How to deal with a specific error when using the Enable-RemoteMailbox cmdlet to create a mailbox in the cloud-based service, for an existing user in an on-premises Active Directory.
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

834 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