我从来没有能够使AllowEmptyString验证属性起作用。
此:
function Get-InputString(
[parameter(mandatory=$true, position=0)][string][AllowEmptyString]$Str
)
{
$Str
}
结果如下:
PS C:\> Get-InputString ''
Unable to find type [AllowEmptyString]: make sure that the assembly containing this type is loaded.
At line:2 char:71
+ [parameter(mandatory=$true, position=0)][string][AllowEmptyString] <<<< $Str
+ CategoryInfo : InvalidOperation: (AllowEmptyString:String) [], RuntimeException
+ FullyQualifiedErrorId : TypeNotFound
答案 0 :(得分:2)
你错过了属性上的parens。见http://technet.microsoft.com/en-us/library/dd347600.aspx
function Get-InputString
{
Param(
[Parameter(Mandatory=$true, Position=0)]
[AllowEmptyString()]
[String]
$Str
)
$Str
}