Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

Direct Report Lookup

$
0
0

Hi Scripting Guys,

I'm trying to search Active Directory (one specific user) so I can not only see all of this user's direct reports, but any of these accounts that have direct reports themselves.  Ideally, I'd like to output their email addresses rather than distinguished names.  This is causing me considerable grief and stress.  I'm hoping you can help.  I've included a script that I found which will give me the entire AD database, but I only need one user (manager).

Thanks

G

'========================

Option Explicit

Dim objRootDSE, strDNSDomain, adoCommand, adoConnection
Dim strBase, strAttributes

' Determine DNS domain name.
Set objRootDSE = GetObject("LDAP://RootDSE")
strDNSDomain = objRootDSE.Get("defaultNamingContext")

' Use ADO to search Active Directory.
Set adoCommand = CreateObject("ADODB.Command")
Set adoConnection = CreateObject("ADODB.Connection")
adoConnection.Provider = "ADsDSOObject"
adoConnection.Open "Active Directory Provider"
Set adoCommand.ActiveConnection = adoConnection
adoCommand.Properties("Page Size") = 100
adoCommand.Properties("Timeout") = 30
adoCommand.Properties("Cache Results") = False

' Search entire domain.
strBase = "<LDAP://" & strDNSDomain & ">"

' Comma delimited list of attribute values to retrieve.
strAttributes = "distinguishedName,directReports"

' Document organization.
Call Reports("Top", "")

' Clean up.
adoConnection.Close

Sub Reports(ByVal strManager, ByVal strOffset)
    ' Recursive subroutine to document organization.

    Dim strDN, adoRecordset, arrReports, strReport
    Dim strFilter, strQuery

    If (strManager = "Top") Then
        ' Search for all managers at top of organizational tree.
        ' These are objects with direct reports but no manager.
        strFilter = "(&(!manager=*)(directReports=*))"
    Else
        ' Output object that reports to previous manager.
        Wscript.Echo strOffset & strManager
        strOffset = strOffset & "    "
        ' Search for all objects that report to this manager.
        strFilter = "(manager=" & strManager & ")"
    End If

    ' Construct the LDAP query.
    strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"

    ' Run the query.
    adoCommand.CommandText = strQuery
    Set adoRecordset = adoCommand.Execute

    ' Enumerate the resulting recordset.
    Do Until adoRecordset.EOF
        ' Retrieve values.
        strDN = adoRecordset.Fields("distinguishedName").Value
        Wscript.Echo strOffset & strDN
        arrReports = adoRecordset.Fields("directReports").Value
        If Not IsNull(arrReports) Then
            For Each strReport In arrReports
                Call Reports(strReport, strOffset & "    ")
            Next
        End If
        adoRecordset.MoveNext
    Loop
    adoRecordset.Close

'==============

End Sub




Viewing all articles
Browse latest Browse all 15028

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>