2026-09-10 21:18:43 +05:30
|
|
|
<#
|
|
|
|
|
.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).
|
|
|
|
|
#>
|
2026-05-08 21:19:52 +02:00
|
|
|
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
|
2026-09-10 21:18:43 +05:30
|
|
|
Message = Get-Translation -Key 'UsernameValidationEmpty'
|
2026-05-08 21:19:52 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-08 23:18:04 +02:00
|
|
|
if (Test-UserNameMatch -UserNameA $normalizedUserName -UserNameB $env:USERNAME) {
|
2026-05-08 21:19:52 +02:00
|
|
|
return [PSCustomObject]@{
|
|
|
|
|
IsValid = $false
|
|
|
|
|
UserName = $normalizedUserName
|
2026-09-10 21:18:43 +05:30
|
|
|
Message = Get-Translation -Key 'UsernameValidationOwnUsername'
|
2026-05-08 21:19:52 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-19 22:06:07 +02:00
|
|
|
if (-not (Test-UserProfileExists -userName $normalizedUserName)) {
|
2026-05-08 21:19:52 +02:00
|
|
|
return [PSCustomObject]@{
|
|
|
|
|
IsValid = $false
|
|
|
|
|
UserName = $normalizedUserName
|
2026-09-10 21:18:43 +05:30
|
|
|
Message = Get-Translation -Key 'UsernameValidationNotFound'
|
2026-05-08 21:19:52 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return [PSCustomObject]@{
|
|
|
|
|
IsValid = $true
|
|
|
|
|
UserName = $normalizedUserName
|
2026-09-10 21:18:43 +05:30
|
|
|
Message = Get-Translation -Key 'UsernameValidationFound' -FormatArgs @($normalizedUserName)
|
2026-05-08 21:19:52 +02:00
|
|
|
}
|
|
|
|
|
}
|