-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGet-ADUserBySamAccountName.ps1
More file actions
108 lines (79 loc) · 3.7 KB
/
Get-ADUserBySamAccountName.ps1
File metadata and controls
108 lines (79 loc) · 3.7 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
107
108
<#
.SYNOPSIS
The "Get-ADUserBySamAccountName" function takes the SamAccountName of Active Directory users and retrieves additional information pertaining to that User Object in Active Directory.
.DESCRIPTION
.EXAMPLE
PS C:\> Get-ADUserBySamAccountName -SamAccountName 'Cyan.Foss'
DistinguishedName : CN=Cyan Foss,OU=Employee,OU=Operators,DC=subd,DC=Roxboard,DC=com
Enabled : True
GivenName : Cyan
Mail : Cyan.Foss@Roxboard.com
Name : Cyan Foss
ObjectClass : user
ObjectGUID : 60e3b20a-4952-2094-83ac-e47de5aae5a2
SamAccountName : Cyan.Foss
SID : S-1-5-21-104866274-201234818-8745921783-64132
Surname : Foss
UserPrincipalName : Cyan.Foss@Roxboard.com
PS C:\> ADUserBySamAccount 'Cyan.Foss'
DistinguishedName : CN=Cyan Foss,OU=Employee,OU=Operators,DC=subd,DC=Roxboard,DC=com
Enabled : True
GivenName : Cyan
Mail : Cyan.Foss@Roxboard.com
Name : Cyan Foss
ObjectClass : user
ObjectGUID : 60e3b20a-4952-2094-83ac-e47de5aae5a2
SamAccountName : Cyan.Foss
SID : S-1-5-21-104866274-201234818-8745921783-64132
Surname : Foss
UserPrincipalName : Cyan.Foss@Roxboard.com
Here we demonstrate the verbose and the fast way to run this function. 'ADUserBySamAccount' is the built-in alias for "Get-ADUserBySamAccountName" 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 'Cyan Foss'
Name AccountFound SamAccountName Mail
---- ------------ -------------- ----
Cyan Foss True Cyan.Foss Cyan.Foss@Roxboard.com
PS C:\> ADNameChecker 'Cyan Foss'
Name AccountFound SamAccountName Mail
---- ------------ -------------- ----
Cyan Foss True Cyan.Foss Cyan.Foss@Roxboard.com
PS C:\> ADNameChecker 'Cyan Foss' | ADUserBySamAccount
DistinguishedName : CN=Cyan Foss,OU=Employee,OU=Operators,DC=subd,DC=Roxboard,DC=com
Enabled : True
GivenName : Cyan
Mail : Cyan.Foss@Roxboard.com
Name : Cyan Foss
ObjectClass : user
ObjectGUID : 60e3b20a-4952-2094-83ac-e47de5aae5a2
SamAccountName : Cyan.Foss
SID : S-1-5-21-104866274-201234818-8745921783-64132
Surname : Foss
UserPrincipalName : Cyan.Foss@Roxboard.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 'ADUserBySamAccount' (the built-in alias for "Get-ADUserBySamAccountName") in order to gain additional information about the user.
.INPUTS
.OUTPUTS
.NOTES
Name: Get-ADUserBySamAccountName.ps1
Author: Travis Logue
Version History: 1.2 | 2022-01-12 | Updated Documentation
Dependencies: Active Directory Module
Notes:
.
#>
function Get-ADUserBySamAccountName {
[CmdletBinding()]
[Alias('ADUserBySamAccount')]
param (
[Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
[string[]]
$SamAccountName
)
begin {}
process {
foreach ($SamAccount in $SamAccountName) {
$UserObject = Get-ADUser -Identity $SamAccount -Properties Mail
Write-Output $UserObject
}
}
end {}
}