Reputation: 81
I'm using a function called Get-ADdirectReports which recursively grabs all users who report to a specific manager (includes other managers and their team members if the manager is responsible for multiple teams). I'm trying to return all AD users and see if they recursively report to the defined manager.
Code
$Manager = Get-ADdirectReports -SamAccountName "ManagerName" | Select SamAccountName
$Users = Get-AdUser -Filter * -Properties * | Select SamAccountName
Foreach ($User in $Users) {
If ($User -Contains $Manager) {Write-Host $User reports to $Manager}
Else
{$User doesn't report to $Manager}
}
Upvotes: 0
Views: 1129
Reputation: 75
Not exactly what you are looking for, but still worth trying I guess
function GetADUserManagerRecursive {
param (
$Identity
)
$UserAD = Get-ADUser $Identity -Properties Manager
$DirectManager = $UserAD.Manager
$DirectManager = Get-ADUser -Identity $DirectManager -Properties Manager
Write-Output $DirectManager.DistinguishedName
if ( $DirectManager.Manager -ne $UserAD.DistinguishedName ) {
GetADUserManagerRecursive -Identity $DirectManager
}
}
Once done, you may want to get all manangers of a specific user recursively, save it in variable and then check if a manager is there in the variable:
$managers = GetADUserManagerRecursive -Identity user
$managers = $managers | select -unique
(Get-ADUser bossname ).SamaccountName -match $managers.SamaccountName
PS Please note you might need changing $DirectManager.Manager -ne $UserAD.DistinguishedName
to $DirectManager.Manager -ne $null
.
I have seen companies where topmost boss has been a manager of himself
Upvotes: 0
Reputation: 37800
Ok, so this is not the most elegant and certainly not the fastest way to do this but it should do what you want.
function Get-ADTopLevelManager{
param(
$identity
)
$result = New-Object System.Collections.ArrayList
$manager = Get-ADUser $identity -Properties Manager
$result.Add($manager) | Out-Null
$managerDN = $manager.Manager
while($manager -ne $null){
$manager = $null
$manager = Get-AdUser -Filter {DistinguishedName -eq $managerDN} -Properties Manager
$managerDN = $manager.Manager
if($manager.SamAccountName -eq $result[-1].SamAccountName){
$manager = $null
}else{
$result.Add($manager) | Out-Null
}
}
$result
}
$allUsers = New-Object System.Collections.ArrayList
foreach($adUser in Get-AdUser -Filter *){
$temp = New-Object PSCustomObject -Property @{'User' = $adUser.SamAccountName; 'Managers' = Get-ADTopLevelManager $adUser.SamAccountName}
$allUsers.Add($temp) | Out-Null
}
So at this point you have a collection of objects that links any given user to all of their managers. So lets say you want to know what users have Manager1 in their chain of command anywhere:
$allUsers | Where-Object{$_.Managers -Contains Manager1}
Or if you want to know the immediate manager of user User1:
($allUsers | Where-Object{$_.User -eq User1}).Managers[0]
Or if you want to know the top level manager for User1:
($allUsers | Where-Object{$_.User -eq User1}).Managers[-1]
Upvotes: 1