Share via


Getting the DNS Name of a Server

Getting the DNS Name of a Server

This content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.

The following example returns the fully qualified DNS name of a specified server.

Visual Basic

'//////////////////////////////////////////////////////////////////////
' Function: getServerIPName()
' Purpose:  Gets the DNS name for a specified server.
'
' Input:    szDomainName:               Domain of the exchange organization
'           szOrganizationName:         Name of Exchange Organization
'           szAdministrativeGroupName:  Name of Administrative Group
'           szServerName:               Name of server to enumerate
'           szUserName:                 Admin Username
'           szUserPwd:                  Admin pwd
'           szDirectoryServer:          Name of the Directory Server
'
' Output:   getServerIPName:            Contains Error code (if any)
'           szIPname:                   Fully qualified DNS name for specified server
'
' Note:  In order for this example to function correctly, it may be necessary to include
' references to the following libraries: Active DS Type Library, Microsoft CDO for
' Exchange Management Library, Microsoft Cluster Service Automation Classes,
' Microsoft CDO for Windows 2000 Library.
'//////////////////////////////////////////////////////////////////////
Public Function getServerIPName(ByVal szDomainName As String, _
                                ByVal szOrganizationName As String, _
                                ByVal szAdministrativeGroupName As String, _
                                ByVal szServerName As String, _
                                ByVal szUserName As String, _
                                ByVal szUserPwd As String, _
                                ByRef szIPname As String, _
                                ByVal szDirectoryServer) As Integer


    Dim objLdap As IADsOpenDSObject
    Dim objContainer As IADs
    Dim szConnString As String
    Dim saAddressArray() As Variant
    Dim iIndex As Integer
    Dim szaDomTokens() As String
    Dim szDomainDN As String

    On Error GoTo errhandler

    ' Put the domain name into an ldap string.
    szaDomTokens = Split(szDomainName, ".", -1, 1)
    szDomainDN = Join(szaDomTokens, ",dc=")
    szDomainDN = "dc=" & szDomainDN
    ' Build up the ldap connection string.

    szConnString = "LDAP://" + szDirectoryServer + "/cn=" + szServerName + _
                   ",cn=servers,cn=" + szAdministrativeGroupName + ",cn=Administrative Groups,cn=" _
                   + szOrganizationName + ",cn=Microsoft Exchange,cn=services,cn=configuration," + _
                   szDomainDN

    ' Open up the directory with the passed credentials.

    Set objLdap = GetObject("LDAP:")

    ' Get a container object from the connection string.

    Set objContainer = objLdap.OpenDSObject(szConnString, _
                                            szUserName, _
                                            szUserPwd, _
                                            ADS_SECURE_AUTHENTICATION)

    ' Copy the network address array out of the DS so we can loop through it.

    saAddressArray = objContainer.Get("networkAddress")

    ' tempstr is an array of addresses, and we need to get the TCP/IP address.
    For iIndex = 0 To UBound(saAddressArray)
        If InStr(saAddressArray(iIndex), "ncacn_ip_tcp") Then
            szIPname = saAddressArray(iIndex)
            Exit For
        End If
    Next

    ' Strip the prefix.

    szIPname = Replace(szIPname, "ncacn_ip_tcp:", "")

    ' Clean up.
    Set objLdap = Nothing
    Set objContainer = Nothing
    getServerIPName = 0
    Exit Function

    ' Error handling.
errhandler:

    Set objLdap = Nothing
    Set objContainer = Nothing
    getServerIPName = 1
    ' Implement error logging here.

    Exit Function

Send us your feedback about the Microsoft Exchange Server 2003 SDK.

Build: June 2007 (2007.618.1)

© 2003-2006 Microsoft Corporation. All rights reserved. Terms of use.