Powershell ValidateScript参数验证

Powershell ValidateScript参数验证,powershell,Powershell,这一行: [ValidateScript({if (!(Test-Path $_) -and (!($_ -like "*.exe"))) { Throw "Specify correct path to executable." } else {$true}})] [String]$installerPath 测试路径验证返回真/假 然而,-like未按预期工作。使用.txt、.msi etc文件类型传递参数无法正确验证。我只需交换if-else块并删除否定(!): 我只需交换if-els

这一行:

[ValidateScript({if (!(Test-Path $_) -and (!($_ -like "*.exe"))) 
{ Throw "Specify correct path to executable." }
else {$true}})]
[String]$installerPath
测试路径
验证返回真/假

然而,
-like
未按预期工作。使用.txt、.msi etc文件类型传递参数无法正确验证。

我只需交换if-else块并删除否定(
):

我只需交换if-else块并删除否定(
):


为了更清楚地验证,我将分割检查并提供不同的错误消息:

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

不需要“else”,因为抛出会导致立即退出。

为了更清楚地验证,我将拆分检查并提供不同的错误消息:

[ValidateScript({
    if(-Not Test-Path $_ ){ throw "$_ does not exist." }
    if(-Not ($_ -like "*.exe")){ throw "Input file must be an executable." }
    $true
})]
[String]
$installerPath
不需要“else”,因为抛出会导致立即退出