简体   繁体   中英

Get organizationUnit from LDAP with VBA

I'm working with Access 2003 and already have a code that extracts a lot of data from LDAP. Here is what I got:

Set rootDSE = GetObject("LDAP://RootDSE")
domainContainer = rootDSE.Get("defaultNamingContext")

conn.provider = "ADSDSOObject"
conn.Open "ADs Provider"

ldapStr = "<LDAP://" & domainContainer & ">;(& (mailnickname=" & nickname & ") (| (&(objectCategory=person)(objectClass=user)(!(homeMDB=*))(!(msExchHomeServerName=*)))(&(objectCategory=person)(objectClass=user)(|(homeMDB=*)(msExchHomeServerName=*))) ));adspath;subtree"
exchangeRS.Open ldapStr, conn, adOpenStatic, adLockReadOnly
exchangeRS.MoveFirst

Do Until exchangeRS.EOF
    Set oUser = GetObject(exchangeRS.fields(0).value)

    ' The properties below are working
    'oUser.firstName
    'oUser.displayName
    'oUser.title
    'oUser.telephoneNumber
    'oUser.mobile
    'oUser.faxNumber
    'oUser.streetAddress
    'oUser.l
    'oUser.postalCode
    'oUser.mail
Next

I'm able to get a lot of informations for each person in the LDAP database. However, I would also like to get the structural unit . However, oUser.organizationUnit doesn't exit and oUser.OU only contains the top unit, which isn't what I want.

Is there any way to list all properties of oUser to find the right one? Is the ldapStr missing something?

Here is an image of what I want to get (sorry it it's in French):

单元

Is it possible that this is not stored in LDAP? If not, any way to get it from Outlook address book? But I would really prefer getting it from LDAP actually, as every other single information is actually there (which tends me to believe this should be there too).

Look at the distinguishedName property. This is a sequence of relative distinguished names (RDN) which will include OUs

Here is the best method I've found for figuring out what data is in what fields. Dump the Schema to Text, then load that text back into Excel, little concatenate action to craft the output lines, then feed it back to VBA for it to dump all of the the data from AD so you can see what information is being stored where.

Step 1: Get your schema fields

Function SchemaToText()
    Set objSysInfo = CreateObject("ADSystemInfo")
    Set objUser = GetObject("LDAP://" & objSysInfo.userName)
    Set objSchema = GetObject("LDAP://schema/user")
    OutFile = Application.DefaultFilePath & "\schemalist.txt"
    Debug.Print "Exporting to " & Application.DefaultFilePath & "\schemalist.txt"
    Open OutFile For Output As #1
    For Each strAttribute In objSchema.MandatoryProperties
        Write #1, strAttribute
    Next
    For Each strAttribute In objSchema.OptionalProperties
        Write #1, strAttribute
    Next
    Close #1
End Function

Step 2: All of your fields will have double quotes around them, so we'll need to remove those. Open schemalist.txt in Notepad (or other text editor of your choice), then use the replace function to find all of the double quotes and delete them

Step 3: Load schemalist.txt into a new spreadsheet in Excel (for the sake of this tutorial, we're assuming it's been placed into cell A1)

Step 4: Build write commands for dumping the field values - In cell B1, input the following:

=Concatenate("Write #1, ","""",".",A1,"|",""""," & .",A1)

You should now have something like this: Write #1, ".cn|" &.cn Write #1, ".cn|" &.cn . Now you just need to fill that concatenate formula down for the entire schema list.

Step 5: Paste the write list into the following formula:

Function UserSchemaDump()
    Set objSysInfo = CreateObject("ADSystemInfo")
    Set objUser = GetObject("LDAP://" & objSysInfo.userName)
    OutFile = Application.DefaultFilePath & "\schemadump.txt"
    Debug.Print "Exporting to " & Application.DefaultFilePath & "\schemadump.txt"
    Open OutFile For Output As #1
    With objUser
        'paste all of the write lines here
        Write #1, ".cn|" & .cn
        Write ...
        Write #1, ".x500uniqueIdentifier|" & .x500uniqueIdentifier
    End With
    Close #1
    Debug.Print "Export complete"
End Function

Step 6: Load schemadump.txt back into Excel, use text to columns to split on the |, then format as table with no headers and sort column 2 to see what fields are being utilized and what data is being placed in each field.

The result of all my testing showed me that this data isn't stored in LDAP.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM