I want to perform a pattern validation of a string value I'm entering and then only continue with the script if it matches but feel like I'm missing something here:
Function Test-ValidatePattern {
[CmdletBinding()]
param (
[Parameter(Mandatory)]
[ValidatePattern(".co.uk|.com")]
[System.String]$DomainName
)
$DomainValid = "1"
Write-Output "$DomainName is a valid domain name format"
}
#Sets forwarding email address, checks what it's set to and outputs this to logfile.
If ($EmailForwarding) {
$ForwardDomain = Read-Host "What domain do you want to forward to? e.g. contoso.com`n"
Test-ValidatePattern -DomainName $ForwardDomain
While (!($DomainValid -eq "1")) {
Write-Host "$ForwardDomain isn't a valid domain. Try again"
$ForwardDomain = Read-Host "What domain do you want to forward to? e.g. contoso.com`n"
}
}
It returns an error as expected when entering a string that doesn't have a .co.uk or .com extension and no error when entering one that does but rather than presenting the message that the domain name is valid it loops back to the while block messages instead of continuing on with the rest of the script.
I've tried -eq, -notmatch, -notlike but still not working as intended.
Any ideas?