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

        Try
            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
            Me.m_sbWarningMsg.Append(ex.Message)
            Return (Nothing)
        End Try

        Return (Me.m_sServer)
    End Function

Thanks!
wjolsonAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AmericomCommented:
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."
Else
    Wscript.Echo strComputer & " is not a global catalog server."
End If

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AmericomCommented:
To identify Domain controllers:

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

Open in new window

0
wjolsonAuthor Commented:
Americom,

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.
Thanks.
William
0
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.
0
AmericomCommented:
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
Next

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Databases

From novice to tech pro — start learning today.