views:

43

answers:

1

I've never been able to get the AllowEmptyString validation attribute to work.

This:

function Get-InputString(
    [parameter(mandatory=$true, position=0)][string][AllowEmptyString]$Str
)
{
    $Str
}

Results in this:

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
A: 

You're missing the parens on the attribute. See http://technet.microsoft.com/en-us/library/dd347600.aspx

function Get-InputString
{
    Param(
        [Parameter(Mandatory=$true, Position=0)]
        [AllowEmptyString()]
        [String]
        $Str
    )

    $Str
}
xcud
Took me a minute to see that. Instead of copying/pasting your repro I was typing out the attribute and subconsciously typing out the parens. Sorry about my useless comment.
xcud
Thanks. It would have been nice if they had the parens in the sample in the PowerShell help.
OldFart
I'm looking at the sample in about_advanced_functions and I see parens - where is the sample you're talking about that lacks the parens?
x0n
sorry, that should be about_functions_advanced_parameters
x0n