我想獲得一個經理下的每個人的名單(如果你願意的話)。我有與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文章。我相信這可行,但我不知道如何讓它搜索指定的經理。任何人都可以把我推向正確的方向嗎?謝謝!
我相信你要加載稱爲 '的ManagedBy' 的AD屬性。嘗試在域控制器上使用adsiedit.msc並查看用戶的內容以查看ldap屬性的值。 –
你說你有使用PowerShell cmdlet的工作代碼 - 爲什麼需要只使用'[ADSI]'類型加速器的替代代碼? –
我可能沒有正確的措辭。我想要做的是在一個變量中指定一個管理器,併爲其尋找功能。我想要使用ADSI,因此人們不必安裝RSAT工具來運行我的腳本。 –