#Requires -Version 5.1 # Pure validation helpers for the WinPE collector. No WinForms dependency so they # are unit-testable headless. Each returns [pscustomobject]@{ Ok=[bool]; Message=[string] }. function New-SmResult([bool]$ok, [string]$msg = '') { [pscustomobject]@{ Ok = $ok; Message = $msg } } $script:SmReserved = @('administrator','guest','system','defaultaccount','wdagutilityaccount','sm-bootstrap') function Test-SmUsername([string]$name) { if ([string]::IsNullOrWhiteSpace($name)) { return New-SmResult $false 'Username is required.' } if ($name.Length -gt 20) { return New-SmResult $false 'Username must be 20 characters or fewer.' } if ($script:SmReserved -contains $name.ToLower()) { return New-SmResult $false 'That username is reserved.' } if ($name -notmatch '^[A-Za-z0-9][A-Za-z0-9 ._-]*$') { return New-SmResult $false 'Username has illegal characters.' } New-SmResult $true } function Test-SmPassword([string]$pw, [string]$confirm) { if ([string]::IsNullOrEmpty($pw)) { return New-SmResult $false 'Password is required.' } if ($pw.Length -lt 8) { return New-SmResult $false 'Password must be at least 8 characters.' } if ($pw -ne $confirm) { return New-SmResult $false 'Passwords do not match.' } New-SmResult $true } function Test-SmPin([string]$pin, [string]$confirm) { if ($pin -notmatch '^[0-9]+$') { return New-SmResult $false 'PIN must be numeric.' } if ($pin.Length -lt 6) { return New-SmResult $false 'PIN must be at least 6 digits.' } if ($pin -ne $confirm) { return New-SmResult $false 'PINs do not match.' } New-SmResult $true } function Test-SmComputerName([string]$name) { if ([string]::IsNullOrWhiteSpace($name)) { return New-SmResult $false 'Computer name is required.' } if ($name.Length -gt 15) { return New-SmResult $false 'Computer name must be 15 characters or fewer.' } if ($name -notmatch '^[A-Za-z0-9-]+$') { return New-SmResult $false 'Computer name: letters, digits, hyphens only.' } New-SmResult $true }