0

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?

1

3 Answers 3

2

I don't understand why you think you need to write a validation script when that is the purpose of the ValidatePattern attribute. Here is an example function:

function sample {
  param(
    [Parameter(Mandatory=$true)]
    [ValidatePattern('(\.co\.uk|\.com)$')]
    [String] $DomainName
  )
  "Your domain name is $DomainName"
}

If you run

sample foo.bar

PowerShell will throw a ParameterBindingValidationException exception because the string you passed as the $DomainName parameter doesn't match the pattern. However, if you run

sample foo.com

You will see the expected output:

Your domain name is foo.com
Sign up to request clarification or add additional context in comments.

7 Comments

I want to act upon the results of the validation script not just be told whether it's valid or not
(?) You can still do that. The point is you don't need to test for the validity of the parameter because the ValidatePattern attribute already does that for you.
I'm obviously misunderstanding something here. If I run "sample foo.bar" I'm thrown an exception as you mention so how do I then get it to prompt for a domain name again rather than simply show the exception and continue regardless?
That's a different matter entirely. If your goal is to allow the user to pass any string to the function and you want to validate and prompt the user if the pattern doesn't match a string, then don't use ValidatePattern. But there's a different way to look at it. If you use ValidatePattern, then the function won't run at all, so the user will need to run the function again with a matching pattern. Either way, you accomplish your goal, but ValidatePattern is a lot less code.
Testing this without the while loop as you've shown above, after I got the exception from a invalid string the script still continues regardless so sounds like I need to use a different approach then. Open to suggestions. Maybe a -Match Comparator in a 'Where' Clause!?
|
1

You're not setting $DomainValid to any value. The scope of the function clears out $DomainValid when it finishes. You'll need to return a value from the Test-ValidatePattern function. For Example:

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"
    return 1
}

#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"
    $DomainValid = 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"
        }
}

1 Comment

Ok thanks for the explanation. I've updated the script as shown but get the same result. A valid format won't show an exception so know it's happy with the input but just keeps going through the While loop instead of proceeding with the rest of the script.
0

I feel I've missed off a few things when explaining what I was after. I simply wanted to make sure the input had a dot present so single words couldn't be entered e.g. "foo.bar" is allowed but "foo" isn't. If not present then the user should be prompted again. Nothing too strict and doesn't stop invalid entries but more so to catch a lazy input.

After tinkering around a bit more the below appears to do what I require.

If ($EmailForwarding) {
    $ForwardDomain = Read-Host "What domain do you want to forward to? e.g. contoso.com`n"
    While (!($ForwardDomain -like "*.*"))
    {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"}}
    "rest of script"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.