2017-07-18 50 views
0

我想获得一个经理下的每个人的名单(如果你愿意的话)。我有与Active Directory模块一起工作的代码,但我无法弄清楚如何使用ADSI来完成此任务。PowerShell递归直接报告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文章。我相信这可行,但我不知道如何让它搜索指定的经理。任何人都可以把我推向正确的方向吗?谢谢!

+0

我相信你要加载称为 '的ManagedBy' 的AD属性。尝试在域控制器上使用adsiedit.msc并查看用户的内容以查看ldap属性的值。 –

+0

你说你有使用PowerShell cmdlet的工作代码 - 为什么需要只使用'[ADSI]'类型加速器的替代代码? –

+0

我可能没有正确的措辞。我想要做的是在一个变量中指定一个管理器,并为其寻找功能。我想要使​​用ADSI,因此人们不必安装RSAT工具来运行我的脚本。 –

回答

0
$Filter = "(manager=<ManagerDN>)"

或者更具体:

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

这将为我指定的经理递归地获取所有直接报告,正确吗? –

+0

该问题并没有完全达到脚本的目的,因为脚本需要用户作为输入,并且您想要在管理器上进行过滤。但是,所有使用特定(直接)管理器的用户都将被枚举,并且输出将包含这些用户的所有(递归)管理器。 – iRon