1- function Get-ADDirectReports
2- {
3- <#
1+ function Get-ADDirectReports {
2+ <#
43 . SYNOPSIS
54 This function retrieve the directreports property from the IdentitySpecified.
65 Optionally you can specify the Recurse parameter to find all the indirect
76 users reporting to the specify account (Identity).
8-
7+
98 . DESCRIPTION
109 This function retrieve the directreports property from the IdentitySpecified.
1110 Optionally you can specify the Recurse parameter to find all the indirect
1211 users reporting to the specify account (Identity).
13-
12+
1413 . NOTES
1514 Francois-Xavier Cat
1615 www.lazywinadmin.com
1716 @lazywinadm
18-
17+
1918 VERSION HISTORY
2019 1.0 2014/10/05 Initial Version
21-
20+
2221 . PARAMETER Identity
2322 Specify the account to inspect
24-
23+
2524 . PARAMETER Recurse
2625 Specify that you want to retrieve all the indirect users under the account
27-
26+
2827 . EXAMPLE
2928 Get-ADDirectReports -Identity Test_director
30-
29+
3130Name SamAccountName Mail Manager
3231---- -------------- ---- -------
3332test_managerB test_managerB test_managerB@la... test_director
3433test_managerA test_managerA test_managerA@la... test_director
35-
34+
3635 . EXAMPLE
3736 Get-ADDirectReports -Identity Test_director -Recurse
38-
37+
3938Name SamAccountName Mail Manager
4039---- -------------- ---- -------
4140test_managerB test_managerB test_managerB@la... test_director
@@ -44,65 +43,54 @@ test_userB2 test_userB2 test_userB2@lazy... test_managerB
4443test_managerA test_managerA test_managerA@la... test_director
4544test_userA2 test_userA2 test_userA2@lazy... test_managerA
4645test_userA1 test_userA1 test_userA1@lazy... test_managerA
47-
46+
4847 #>
49- [CmdletBinding ()]
50- PARAM (
51- [Parameter (Mandatory )]
52- [String []]$Identity ,
53- [Switch ]$Recurse
54- )
55- BEGIN
56- {
57- TRY
58- {
59- IF (-not (Get-Module - Name ActiveDirectory)) { Import-Module - Name ActiveDirectory - ErrorAction ' Stop' - Verbose:$false }
60- }
61- CATCH
62- {
63- Write-Verbose - Message " [BEGIN] Something wrong happened"
64- Write-Verbose - Message $Error [0 ].Exception.Message
65- }
66- }
67- PROCESS
68- {
69- foreach ($Account in $Identity )
70- {
71- TRY
72- {
73- IF ($PSBoundParameters [' Recurse' ])
74- {
75- # Get the DirectReports
76- Write-Verbose - Message " [PROCESS] Account: $Account (Recursive)"
77- Get-Aduser - identity $Account - Properties directreports |
78- ForEach-Object - Process {
79- $_.directreports | ForEach-Object - Process {
80- # Output the current object with the properties Name, SamAccountName, Mail and Manager
81- Get-ADUser - Identity $PSItem - Properties mail, manager, DistinguishedName | Select-Object - Property Name, SamAccountName, DistinguishedName, Mail, @ { Name = " Manager" ; Expression = { (Get-Aduser - identity $psitem.manager ).samaccountname } } | Where-Object { $_.DistinguishedName -like " *,OU=Employees,OU=People,DC=DOMAINNAME,DC=org" }
82- # Gather DirectReports under the current object and so on...
83- Get-ADDirectReports - Identity $PSItem - Recurse
84- }
85- }
86- }# IF($PSBoundParameters['Recurse'])
87- IF (-not ($PSBoundParameters [' Recurse' ]))
88- {
89- Write-Verbose - Message " [PROCESS] Account: $Account "
90- # Get the DirectReports
91- Get-Aduser - identity $Account - Properties directreports | Select-Object - ExpandProperty directReports |
92- Get-ADUser - Properties mail, manager | Select-Object - Property Name, SamAccountName, Mail, @ { Name = " Manager" ; Expression = { (Get-Aduser - identity $psitem.manager ).samaccountname } }
93- }# IF (-not($PSBoundParameters['Recurse']))
94- }# TRY
95- CATCH
96- {
97- Write-Verbose - Message " [PROCESS] Something wrong happened"
98- Write-Verbose - Message $Error [0 ].Exception.Message
99- }
100- }
101- }
102- END
103- {
104- Remove-Module - Name ActiveDirectory - ErrorAction ' SilentlyContinue' - Verbose:$false | Out-Null
105- }
48+ [CmdletBinding ()]
49+ PARAM (
50+ [Parameter (Mandatory )]
51+ [String []]$Identity ,
52+ [Switch ]$Recurse
53+ )
54+ BEGIN {
55+ TRY {
56+ IF (-not (Get-Module - Name ActiveDirectory)) { Import-Module - Name ActiveDirectory - ErrorAction ' Stop' - Verbose:$false }
57+ } CATCH {
58+ Write-Verbose - Message ' [BEGIN] Something wrong happened'
59+ Write-Verbose - Message $Error [0 ].Exception.Message
60+ }
61+ }
62+ PROCESS {
63+ foreach ($Account in $Identity ) {
64+ TRY {
65+ IF ($PSBoundParameters [' Recurse' ]) {
66+ # Get the DirectReports
67+ Write-Verbose - Message " [PROCESS] Account: $Account (Recursive)"
68+ Get-ADUser - Identity $Account - Properties directreports |
69+ ForEach-Object - Process {
70+ $_.directreports | ForEach-Object - Process {
71+ # Output the current object with the properties Name, SamAccountName, Mail and Manager
72+ Get-ADUser - Identity $PSItem - Properties mail, manager, DistinguishedName | Select-Object - Property Name, SamAccountName, DistinguishedName, Mail, @ { Name = ' Manager' ; Expression = { (Get-ADUser - Identity $psitem.manager ).samaccountname } } | Where-Object { $_.DistinguishedName -like ' *,OU=Employees,OU=People,DC=DOMAINNAME,DC=org' }
73+ # Gather DirectReports under the current object and so on...
74+ Get-ADDirectReports - Identity $PSItem - Recurse
75+ }
76+ }
77+ }# IF($PSBoundParameters['Recurse'])
78+ IF (-not ($PSBoundParameters [' Recurse' ])) {
79+ Write-Verbose - Message " [PROCESS] Account: $Account "
80+ # Get the DirectReports
81+ Get-ADUser - Identity $Account - Properties directreports | Select-Object - ExpandProperty directReports |
82+ Get-ADUser - Properties mail, manager | Select-Object - Property Name, SamAccountName, Mail, @ { Name = ' Manager' ; Expression = { (Get-ADUser - Identity $psitem.manager ).samaccountname } }
83+ }# IF (-not($PSBoundParameters['Recurse']))
84+ }# TRY
85+ CATCH {
86+ Write-Verbose - Message ' [PROCESS] Something wrong happened'
87+ Write-Verbose - Message $Error [0 ].Exception.Message
88+ }
89+ }
90+ }
91+ END {
92+ Remove-Module - Name ActiveDirectory - ErrorAction ' SilentlyContinue' - Verbose:$false | Out-Null
93+ }
10694}
10795
10896<#
@@ -111,4 +99,4 @@ Get-ADDirectReports -Identity Test_director
11199
112100# Find all Indirect user reporting to Test_director
113101Get-ADDirectReports -Identity Test_director -Recurse
114- #>
102+ #>
0 commit comments