forked from PoeBlu/powershell-scripts
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGet-ADDirectReport.ps1
114 lines (102 loc) · 3.95 KB
/
Get-ADDirectReport.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
function Get-ADDirectReports
{
<#
.SYNOPSIS
This function retrieve the directreports property from the IdentitySpecified.
Optionally you can specify the Recurse parameter to find all the indirect
users reporting to the specify account (Identity).
.DESCRIPTION
This function retrieve the directreports property from the IdentitySpecified.
Optionally you can specify the Recurse parameter to find all the indirect
users reporting to the specify account (Identity).
.NOTES
Francois-Xavier Cat
www.lazywinadmin.com
@lazywinadm
VERSION HISTORY
1.0 2014/10/05 Initial Version
.PARAMETER Identity
Specify the account to inspect
.PARAMETER Recurse
Specify that you want to retrieve all the indirect users under the account
.EXAMPLE
Get-ADDirectReports -Identity Test_director
Name SamAccountName Mail Manager
---- -------------- ---- -------
test_managerB test_managerB test_managerB@la... test_director
test_managerA test_managerA test_managerA@la... test_director
.EXAMPLE
Get-ADDirectReports -Identity Test_director -Recurse
Name SamAccountName Mail Manager
---- -------------- ---- -------
test_managerB test_managerB test_managerB@la... test_director
test_userB1 test_userB1 test_userB1@lazy... test_managerB
test_userB2 test_userB2 test_userB2@lazy... test_managerB
test_managerA test_managerA test_managerA@la... test_director
test_userA2 test_userA2 test_userA2@lazy... test_managerA
test_userA1 test_userA1 test_userA1@lazy... test_managerA
#>
[CmdletBinding()]
PARAM (
[Parameter(Mandatory)]
[String[]]$Identity,
[Switch]$Recurse
)
BEGIN
{
TRY
{
IF (-not (Get-Module -Name ActiveDirectory)) { Import-Module -Name ActiveDirectory -ErrorAction 'Stop' -Verbose:$false }
}
CATCH
{
Write-Verbose -Message "[BEGIN] Something wrong happened"
Write-Verbose -Message $Error[0].Exception.Message
}
}
PROCESS
{
foreach ($Account in $Identity)
{
TRY
{
IF ($PSBoundParameters['Recurse'])
{
# Get the DirectReports
Write-Verbose -Message "[PROCESS] Account: $Account (Recursive)"
Get-Aduser -identity $Account -Properties directreports |
ForEach-Object -Process {
$_.directreports | ForEach-Object -Process {
# Output the current object with the properties Name, SamAccountName, Mail and Manager
Get-ADUser -Identity $PSItem -Properties mail, manager | Select-Object -Property Name, SamAccountName, Mail, @{ Name = "Manager"; Expression = { (Get-Aduser -identity $psitem.manager).samaccountname } }
# Gather DirectReports under the current object and so on...
Get-ADDirectReports -Identity $PSItem -Recurse
}
}
}#IF($PSBoundParameters['Recurse'])
IF (-not ($PSBoundParameters['Recurse']))
{
Write-Verbose -Message "[PROCESS] Account: $Account"
# Get the DirectReports
Get-Aduser -identity $Account -Properties directreports | Select-Object -ExpandProperty directReports |
Get-ADUser -Properties mail, manager | Select-Object -Property Name, SamAccountName, Mail, @{ Name = "Manager"; Expression = { (Get-Aduser -identity $psitem.manager).samaccountname } }
}#IF (-not($PSBoundParameters['Recurse']))
}#TRY
CATCH
{
Write-Verbose -Message "[PROCESS] Something wrong happened"
Write-Verbose -Message $Error[0].Exception.Message
}
}
}
END
{
Remove-Module -Name ActiveDirectory -ErrorAction 'SilentlyContinue' -Verbose:$false | Out-Null
}
}
<#
# Find all direct user reporting to Test_director
Get-ADDirectReports -Identity Test_director
# Find all Indirect user reporting to Test_director
Get-ADDirectReports -Identity Test_director -Recurse
#>