2016-06-10 57 views
1

當函數參數使用驗證集時,我似乎無法使splatting工作。使用ValidateSet PowerShell Splatting

下面的例子產生以下錯誤:

WhichFruit : Cannot validate argument on parameter 'fruit'. The argument "System.Collections.Hashtable" does not belong to the set "Apple,Orange" specified by the ValidateSet attribute. Supply an argument that is in the set and then try the command again.

Function WhichFruit { 
    param(
    [ValidateSet("Apple","Orange")] 
    [string]$fruit 
    ) 

    Write-Host $fruit 
} 

$params = @{ fruit = "Apple" } 

WhichFruit $params 

回答

1

您沒有使用潑灑操作@

Function WhichFruit { 
    param(
    [ValidateSet("Apple","Orange")] 
    [string]$fruit 
    ) 

    Write-Host $fruit 
} 

$params = @{ fruit = "Apple" } 

WhichFruit @params