Skip to content

Commit 6218e91

Browse files
committed
Add Test-UsernameConventionMatch function to validate username conventions based on AD attributes
1 parent 22eccca commit 6218e91

1 file changed

Lines changed: 80 additions & 0 deletions

File tree

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
function Test-UsernameConventionMatch {
2+
<#
3+
.SYNOPSIS
4+
Verifies if a username matches specific naming conventions based on AD attributes.
5+
6+
.DESCRIPTION
7+
This function retrieves a user from Active Directory and checks if their sAMAccountName follows either
8+
the "initials" convention or the "FirstInitial + SurName" convention based on their GivenName and SurName.
9+
10+
.PARAMETER Identity
11+
The identity of the AD user to check. Can be a sAMAccountName, DistinguishedName, GUID, or SID.
12+
13+
.EXAMPLE
14+
Test-UsernameConventionMatch -Identity "jdoe"
15+
16+
.EXAMPLE
17+
Get-ADUser -Filter {Department -eq "IT"} | Test-UsernameConventionMatch
18+
19+
.OUTPUTS
20+
PSCustomObject containing the validation results.
21+
22+
.NOTES
23+
Requires the ActiveDirectory module.
24+
#>
25+
[CmdletBinding()]
26+
param(
27+
[Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
28+
[Alias('SamAccountName', 'UserName', 'DistinguishedName')]
29+
[string[]]$Identity
30+
)
31+
32+
begin {
33+
# Import the Active Directory module if not already loaded
34+
if (-not (Get-Module -Name ActiveDirectory)) {
35+
Import-Module ActiveDirectory -ErrorAction Stop
36+
}
37+
}
38+
39+
process {
40+
try {
41+
42+
# Get the user from Active Directory
43+
$ADUser = Get-ADUser -Identity $Identity -Properties GivenName, SurName -ErrorAction Stop
44+
45+
# Extract the necessary information
46+
$Username = $ADUser.SamAccountName
47+
$FirstName = $ADUser.GivenName
48+
$MiddleInitial = $ADUser.Initial
49+
$LastName = $ADUser.SurName
50+
51+
# Validate that we have first and last names
52+
if ([string]::IsNullOrEmpty($FirstName) -or [string]::IsNullOrEmpty($LastName)) {
53+
Write-Warning "User $Identity does not have both GivenName and SurName attributes populated in AD."
54+
return
55+
}
56+
57+
# Prepare the expected formats
58+
$Initials = ($FirstName[0] + $MiddleInitial + $LastName[0]).ToLower()
59+
$FirstInitialLastName = ($FirstName[0] + $LastName).ToLower()
60+
61+
# Check if the username matches any of the conventions
62+
$MatchesInitials = $Username -eq $Initials
63+
$MatchesFirstInitialLastName = $Username -eq $FirstInitialLastName
64+
65+
# Return the results as an object
66+
[PSCustomObject]@{
67+
Username = $Username
68+
FirstName = $FirstName
69+
LastName = $LastName
70+
MatchesInitialsConvention = $MatchesInitials
71+
MatchesFirstInitialLastNameConvention = $MatchesFirstInitialLastName
72+
IsConventionMatch = $MatchesInitials -or $MatchesFirstInitialLastName
73+
ExpectedInitialsFormat = $Initials
74+
ExpectedFirstInitLastNameFormat = $FirstInitialLastName
75+
}
76+
} catch {
77+
Write-Error "Failed to process user '$Identity': $_"
78+
}
79+
}
80+
}

0 commit comments

Comments
 (0)