Powershell - [AllowEmptyString()] not working -
i have parameter follows:
[parameter(mandatory = false, valuefrompipelinebypropertyname = true)] [allowemptystring()] [validateset("1", "2", "3", "4", "5", "6", "critical", "important", "high", "medium", "low", "lowest")] public string priority { get; set; }
when run command -priority ""
, not work
i know can skip argument, real problem me when execute command using pipe import-csv
if csv not have value, import-csv updates parameter value empty , following error:
cannot validate argument on parameter 'priority'. argument "" not belong set "1,2,3,4,5,6,critical,important,high,med ium,low,lowest" specified validateset attribute. supply argument in set , try command again.
if extend validation set include "", the command if invoked individually runs successfully, still not work if pipe import-csv
i have tried allownull
attribute, still same error
update: after discussing garath, appears that
allowemptystring not work, instead use "" in validateset
the question still remains unanswered - why doesnt
[allowemptystring()]
work?csv files must have trialing commas if there no values
here seems if trailing commas not there
import-csv
passes null command , fails validation givenvalidateset
when trailing commas present, seems empty passed parameter acceptable set includes ""
i simulate problem in simple ps1 script, , working (saved fun2.ps1):
param( [parameter(mandatory=$false, valuefrompipelinebypropertyname=$true)] [string] [validateset("","1", "2", "3", "4", "5", "6")] $a ) write-host $a
the csv below (doc.txt):
"colname" "2" "" "3"
and powershell command is:
import-csv .\doc.txt | %{.\fun2.ps1 $_.colname}
the outuput above commmand is:
ps c:\ps> import-csv .\doc.txt | %{.\fun2.ps1 $_.colname} 2 3
so remove [allowemptystring()]
, change validate set to:
[validateset("", "1", "2", "3", "4", "5", "6", "critical", "important", "high", "medium", "low", "lowest")]
Comments
Post a Comment