-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanagerTreeStructure.ps1
52 lines (50 loc) · 1.56 KB
/
managerTreeStructure.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
function addobject($finalreport,$emp,$type)
{
$tempReport=$null
$tempReport=[pscustomobject]@{
'Name'=$emp.name
'Emp ID'=$emp.samaccountname
'Email'=$emp.userprincipalname
'Manager'=$type
}
return $tempReport
}
#variables
$stack = New-Object System.Collections.Stack
$finalreport=@()
Clear-Host
$main=read-host -Prompt "Enter samaccount name"
$main_result=get-aduser $main
$main_result_DN=(get-aduser $main).distinguishedname
$managedbby=Get-ADUser -Filter { Manager -eq $main_result_dn} | Select-Object Name,samaccountname,distinguishedname,userprincipalname
if($null -eq $managedbby)
{
$tempReport=addobject $finalreport $main_result $false
$finalreport+=$tempReport
}
else
{
$tempReport=addobject $finalreport $main_result $true
$finalreport+=$tempReport
$managedbby | ForEach-Object { $stack.Push($_) }
}
while($stack.Count -gt 0 -and ($item = $stack.Pop()))
{
$employee=$null
$DN=$($item.distinguishedname)
$employee=Get-ADUser -Filter { Manager -eq $DN } | Select-Object Name,samaccountname,distinguishedname,userprincipalname
if($null -ne $employee)
{
Write-Host "Processing manager $($item.samaccountname)"
$tempReport=addobject $finalreport $item $true
$finalreport+=$tempReport
$employee | ForEach-Object { $stack.Push($_) }
}
else
{
Write-Host "Processing employee $($item.samaccountname)"
$tempReport=addobject $finalreport $item $false
$finalreport+=$tempReport
}
}
$finalreport