-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGet-ADManager.ps1
More file actions
106 lines (72 loc) · 3.79 KB
/
Get-ADManager.ps1
File metadata and controls
106 lines (72 loc) · 3.79 KB
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
<#
.SYNOPSIS
The "Get-ADManager" function takes the SamAccountName of Active Directory users and determines who the corresponding Manager is by evaluating the "Manager" property of the User Object in Active Directory.
.DESCRIPTION
.EXAMPLE
PS C:\> Get-ADManager -SamAccountName 'Cyan.Foss' | ft
UserName UserSamAccountName ManagerName SamAccountName ManagerMail
-------- ------------------ ----------- -------------- -----------
Cyan Foss Cyan.Foss Mark Dunkirk Mark.Dunkirk Mark.Dunkirk@MyDomain.com
PS C:\> ADManager 'Cyan.Foss' | ft
UserName UserSamAccountName ManagerName SamAccountName ManagerMail
-------- ------------------ ----------- -------------- -----------
Cyan Foss Cyan.Foss Mark Dunkirk Mark.Dunkirk Mark.Dunkirk@MyDomain.com
Here we demonstrate the verbose and the fast way to run this function. 'ADManager' is the built-in alias for "Get-ADManager" and we show here how we can use it to get the same results as we did when running the function using the full name (the first example) along with an explicit reference to the "-SamAccountName" parameter.
.EXAMPLE
PS C:\> Invoke-ADUserNameChecker -Name 'Tom Storkin'
Name AccountFound SamAccountName Mail
---- ------------ -------------- ----
Tom Storkin True Tom.Storkin Tom.Storkin@Roxboard.com
PS C:\> ADNameChecker 'Tom Storkin'
Name AccountFound SamAccountName Mail
---- ------------ -------------- ----
Tom Storkin True Tom.Storkin Tom.Storkin@Roxboard.com
PS C:\> ADNameChecker -Name 'Tom Storkin' | Get-ADManager | ft
UserName UserSamAccountName ManagerName SamAccountName ManagerMail
-------- ------------------ ----------- -------------- -----------
Tom Storkin Tom.Storkin Bilheel Peck Bilheel.Peck Bilheel.Peck@MyDomain.com
Here we demonstrate the use of a chain of functions and how they can be used together. We first demonstrate the use of "Invoke-ADUserNameChecker" and its built-in alias 'ADNameChecker' in order to retrieve the SamAccountName for a specic user's Name. From there, we are able to pipe that object to "Get-ADManager" in order to gain additional information about the user.
.INPUTS
.OUTPUTS
.NOTES
Name: Get-ADManager.ps1
Author: Travis Logue
Version History: 1.2 | 2021-09-24 | Added 'ManagerMail' Property
Dependencies: Active Directory Module
Notes:
.
#>
function Get-ADManager {
[CmdletBinding()]
[Alias('ADManager')]
param (
[Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
[string[]]
$SamAccountName
)
begin {}
process {
foreach ($SamAccount in $SamAccountName) {
$UserObject = Get-ADUser -Filter { SamAccountName -eq $SamAccount } -Properties Manager
if ($null -like $UserObject) {
Write-Host "`nA User Object with that SamAccountName was not found in Active Directory.`n" -BackgroundColor Black -ForegroundColor Yellow
}
else {
$IteratorUserObject = $UserObject
do {
$IteratorUserObject = Get-ADUser -Filter { DistinguishedName -eq $IteratorUserObject.Manager } -Properties Manager, DirectReports, Mail
} until ($UserObject.DistinguishedName -in $IteratorUserObject.DirectReports)
$prop = [ordered]@{
UserName = $UserObject.Name
UserSamAccountName = $UserObject.SamAccountName
ManagerName = $IteratorUserObject.Name
SamAccountName = $IteratorUserObject.SamAccountName
ManagerMail = $IteratorUserObject.Mail
}
$obj = New-Object -TypeName psobject -Property $prop
Write-Output $obj
}
}
}
end {}
}