mirror of
https://github.com/Raphire/Win11Debloat.git
synced 2026-10-07 22:16:43 +00:00
Credit to @jmolinap2 for their work in #643, which was used as inspiration.
51 lines
1.7 KiB
PowerShell
51 lines
1.7 KiB
PowerShell
<#
|
|
.SYNOPSIS
|
|
Validates a username for the "apply changes to another user" deployment target.
|
|
|
|
.DESCRIPTION
|
|
Rejects an empty name, the current user's own name (that's the "Current User" option
|
|
instead), and a name with no matching local profile, in that order.
|
|
|
|
.OUTPUTS
|
|
PSCustomObject with IsValid (bool), UserName (the trimmed input), and Message (a
|
|
translated string describing the validation result, success or failure alike).
|
|
#>
|
|
function Test-TargetUserName {
|
|
param(
|
|
[AllowNull()]
|
|
[AllowEmptyString()]
|
|
[string]$UserName
|
|
)
|
|
|
|
$normalizedUserName = if ($null -ne $UserName) { $UserName.Trim() } else { '' }
|
|
|
|
if ([string]::IsNullOrWhiteSpace($normalizedUserName)) {
|
|
return [PSCustomObject]@{
|
|
IsValid = $false
|
|
UserName = $normalizedUserName
|
|
Message = Get-Translation -Key 'UsernameValidationEmpty'
|
|
}
|
|
}
|
|
|
|
if (Test-UserNameMatch -UserNameA $normalizedUserName -UserNameB $env:USERNAME) {
|
|
return [PSCustomObject]@{
|
|
IsValid = $false
|
|
UserName = $normalizedUserName
|
|
Message = Get-Translation -Key 'UsernameValidationOwnUsername'
|
|
}
|
|
}
|
|
|
|
if (-not (Test-UserProfileExists -userName $normalizedUserName)) {
|
|
return [PSCustomObject]@{
|
|
IsValid = $false
|
|
UserName = $normalizedUserName
|
|
Message = Get-Translation -Key 'UsernameValidationNotFound'
|
|
}
|
|
}
|
|
|
|
return [PSCustomObject]@{
|
|
IsValid = $true
|
|
UserName = $normalizedUserName
|
|
Message = Get-Translation -Key 'UsernameValidationFound' -FormatArgs @($normalizedUserName)
|
|
}
|
|
} |