AllowEmptyString有一些技巧吗?

时间:2010-06-22 19:41:39

标签: powershell powershell-v2.0

我从来没有能够使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

1 个答案:

答案 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
}