我有一个参数如下:
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true)]
[AllowEmptyString()]
[ValidateSet("1", "2", "3", "4", "5", "6", "Critical", "Important", "High", "Medium", "Low", "Lowest")]
public string Priority { get; set; }当我使用-Priority ""运行命令时,它不起作用
我知道我可以跳过这个参数,但真正的问题是当我使用import-csv中的管道执行命令时。
如果我的csv没有任何值,Import将我的参数值更新为空,因此我得到以下错误:
Cannot validate argument on parameter 'Priority'. The argument "" does not belong to the set "1,2,3,4,5,6,Critical,Important,High,Med
ium,Low,Lowest" specified by the ValidateSet attribute. Supply an argument that is in the set and then try the command again.如果我将我的验证集扩展为包含"",则如果单独调用该命令,则该命令将成功运行,但是,如果我将其管道输送到Import-Csv,则该命令仍然不能工作。
我也尝试过AllowNull属性,但仍然是相同的错误
更新:在与Garath讨论之后,我似乎
[AllowEmptyString()]不起作用?Import-Csv将null传递给命令,这会导致ValidateSet提供的验证失败。
当出现尾随逗号时,它似乎是空的传递给参数,该参数可以接受,因为集合包括"“。发布于 2015-08-05 22:19:48
AllowEmptyString在工作。如果不是的话,你就会收到一条错误信息,比如
不能将参数参数“优先级”绑定,因为它是空字符串。
是ValidateSet验证导致了问题,因为您的集合不包含空字符串。
我觉得你不需要这两种证明。删除AllowEmptyString()验证,并将""作为一个元素添加到您的设置中。
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true)]
[ValidateSet("", "1", "2", "3", "4", "5", "6", "Critical", "Important", "High", "Medium", "Low", "Lowest")]
[string] Priorityhttps://stackoverflow.com/questions/16663044
复制相似问题