1

This line:

[ValidateScript({if (!(Test-Path $_) -and (!($_ -like "*.exe"))) 
{ Throw "Specify correct path to executable." }
else {$true}})]
[String]$installerPath

The Test-Path validation returns True/False.

However ! -like is not working as expected. Passing arguments with .txt, .msi etc file type does not validate correctly.

2 Answers 2

4

For clearer validation, I would split up the checks and provide different error messages:

[ValidateScript({
    if(-Not Test-Path $_ ){ throw "$_ does not exist." }
    if(-Not ($_ -like "*.exe")){ throw "Input file must be an executable." }
    $true
})]
[String]
$installerPath

No "else" is needed since the throw causes an immediate exit.

Sign up to request clarification or add additional context in comments.

Comments

3

I would simply swap the if-else block and remove the negation (!):

[ValidateScript(
{
    if ((Test-Path $_) -and ($_ -like "*.exe")) 
    { 
        $true

    }
    else 
    {
        Throw "Specify correct path to executable." 
    }
})

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.