Frage

I'm creating a csv type org chart and was just wondering what would be the preferred to retrieve a users manager, manager's manager, ... etc up to the highest position. Currently i'm using:

[string]$man = $userEntry.manager
    [array]$manName = $man.split('=,')
    $manager = $manName[1]
    $item.Cells.Item($i,1) = $userEntry.name.value
    $item.Cells.Item($i,2) = $userEntry.description.value
    $item.Cells.Item($i,3) = $manager.ToString()

then running get-QADobject to find the next manager by their DN.

but there must be a much cleaner way!

Thanks

War es hilfreich?

Lösung

If I'm understanding you correctly, you want to follow the chain of command all the way up to the very top (where presumably that person has no manager?). In that case, you need to recursively walk up the tree.

Untested pseudocode as I don't have a domain handy at the moment to test with:

Function Get-Manager {
params(
[string]$username
)
$userEntry = get-qaduser $username
[string]$man = $userEntry.manager
if (-not ($man -eq $null)) {
    [array]$manName = $man.split('=,')
    $manager = $manName[1]
    "$manager is the manager of $username";
    Get-Manager $manager
}
}

This will come to a halt once a user has no manager. In my organization's case, our CEO is listed as his own manager, so I'd change the above code to look for the manager to be non-null or equal to the user, so that either of those conditions being true broke the loop.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top