Platform SDK: Active Directory, ADSI, and Directory Services

Getting the Organization and Site Name from a Server

Getting the Organization name from the server is easy. There are two ways to do this:

Dim objRoot As IADs
Set objRoot = GetObject("LDAP://Server")
Debug.Print objRoot.Get("o")

Or

Dim objRoot As IADs
Set objRoot = GetObject("LDAP://Server/rootDSE")
Debug.Print objRoot.Get("defaultNamingContext")

Getting the site in which the server resides requires that you search the entire subtree for a computer object with the server name we are looking for. Once you get the distinguished name of the server, in the form "cn=ServerName, cn=Servers, cn=Configuration, o=SiteName,o=OrgName", parse it out for the site name.

Dim ADOConn As ADODB.Connection
Dim ADOCommand As New Command
Dim RS As ADODB.Recordset
Dim strPath As String

Set ADOConn = CreateObject("ADODB.Connection")
ADOConn.Provider = "ADSDSOObject"
ADOConn.Open "Active Directory Provider"

Set ADOCommand.ActiveConnection = ADOConn
ADOCommand.CommandText = _
"<LDAP://Server>;(&(rdn=ServerName)(objectClass=Computer)) _
;distinguishedName;subtree"
Set RS = ADOCommand.Execute

While Not RS.EOF
    'Store the distinguished name
    strPath = RS.Fields(0)
    'Find the "ou="
    nStart = InStr(strPath, "ou=")
    'Add the length of "ou="
    nStart = nStart + Len("ou=")
    'Find the next comma
    nEnd = InStr(nStart, strPath, ",")
    'The site name is between the "ou=" and the next comma
    Debug.Print Mid(strPath, nStart, nEnd - nStart)
    RS.MoveNext
Wend

RS.Close
Set ADOConn = Nothing
Set ADOCommand = Nothing
Set RS = Nothing

Note: This examples are specific to Exchange Server version 5.5 and below, and are not upwardly compatible with Exchange 6.0. Management and access of Exchange 6.0 Servers should be made through the CDO Exchange Management interfaces instead.