function foo
{
param(
[parameter(Mandatory=$true)]
[ValidateSet('Test1','Test2','Test3')]
[String]$ChooseOne= 'Test1',
)
do xxxxxxxxxxxxxxxx
{
foo -ChooseOne Test9
我正在使用一組必需參數的powershell函數。該驗證組的偉大工程,然而由於驗證是集..如果輸入的選項不是列出的選項之一。我會得到一個錯誤象下面這樣:失敗的PowerShell ValidateSet - 錯誤操作將變量設置爲默認值?
foo: Cannot validate argument on parameter 'ChooseOne'. The argument "Test9" does not belong to the set "Test1,Test2,Test3" specified by the ValidateSet attribute. Supply an argument that is in the set and then try the
command again.
At line:1 char:1
+ foo
+ ~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [foo], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,foo
有沒有一種辦法有一個錯誤的行動來設置委託變量爲它以前定義的默認值?因此,在這種情況下
它會默認$ChooseOne
爲「Test1
」
有大量的高級函數示例使用參數及其各種選項。示例:http://stackoverflow.com/questions/39154887/set-a-default-value-for-variable-in-powershell和[引用TechNet](https://social.technet.microsoft.com/wiki/內容/文章/ 15994.powershell-advanced-function-parameter-attributes.aspx) – user4317867