2017-04-25 6 views
1

このライン:ValidateScriptパラメータの検証

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

Test-Path検証がFalse/Trueを返します。予想通りしかし

!-likeが機能していません。 .txt、.msiなどのファイルタイプで引数を渡すと、正しく検証されません。

私は単に スワップのif-elseブロックと 否定( !)を削除します

答えて

3

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

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

明確に検証するために、私は小切手を分割し、異なるエラーメッセージを提供する:

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

スローが即時終了させますので、ノー「他」必要とされている 。

関連する問題