2017-07-18 45 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