PowerShell 递归直接报告 ADSI

PowerShell recursive direct reports ADSI

我正在尝试获取经理手下的每个人的名单(如果您愿意,可以控制范围)。我有适用于 Active Directory 模块的代码,但我无法弄清楚如何使用 ADSI 来完成它。

我试过使用此代码启动:

Function GetManager($Manager, $Report)
{
    # Output this manager and direct report.
    """$Manager"",""$Report""" | Out-File -FilePath $File -Append

    # Find the manager of this manager.
    $User = [ADSI]"LDAP://$Manager"
    $NextManager = $User.manager
    If ($NextManager -ne $Null)
    {
        # Check for circular hierarchy.
        If ($NextManager -eq $Report) {"Circular hierarchy found with $Report"}
        Else
        {
            GetManager $NextManager $Report
        }
    }
}

$D = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()
$Domain = [ADSI]"LDAP://$D"
$Searcher = New-Object System.DirectoryServices.DirectorySearcher
$Searcher.PageSize = 200
$Searcher.SearchScope = "subtree"
$Searcher.PropertiesToLoad.Add("distinguishedName") > $Null
$Searcher.PropertiesToLoad.Add("manager") > $Null
$Searcher.SearchRoot = "LDAP://" + $Domain.distinguishedName

$File = ".\ADOrganization.csv"
"Organization: $D"  | Out-File -FilePath $File
"Manager,Direct Report" | Out-File -FilePath $File -Append

# Find all direct reports, objects with a manager.
$Filter = "(manager=*)"

# Run the query.
$Searcher.Filter = $Filter

$Results = $Searcher.FindAll()

ForEach ($Result In $Results)
{
    $ReportDN = $Result.Properties.Item("distinguishedName")
    $ManagerDN = $Result.Properties.Item("manager")
    GetManager $ManagerDN $ReportDN
}

本文来自此处 https://social.technet.microsoft.com/Forums/windows/en-US/7bc3d133-e2b3-4904-98dd-b33993db628a/recursively-select-all-subordinates-for-all-users-from-ad?forum=winserverpowershell 的文章。我确信这有效,但我不知道如何让它搜索指定的经理。谁能把我推向正确的方向?谢谢!

$Filter = "(manager=<i><ManagerDN></i>)"

或更具体:

$Filter = "(manager=CN=<i><ManagerCN></i>,OU=<i><ManagerOU></i>,$($Domain.distinguishedName))"