2010-06-22 2 views
2

Je n'ai jamais réussi à obtenir l'attribut de validation AllowEmptyString pour fonctionner.Y at-il une astuce à AllowEmptyString

Ce:

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

Résultats: dans cette

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 

Répondre

2

Vous êtes absent les parens sur l'attribut. Voir http://technet.microsoft.com/en-us/library/dd347600.aspx

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

    $Str 
} 
+0

Il m'a fallu une minute pour voir ça. Au lieu de copier/coller votre repro, je tapais l'attribut et je tapais inconsciemment les parens. Désolé pour mon commentaire inutile. – xcud

+0

Merci. Cela aurait été bien s'ils avaient les parenthèses dans l'échantillon dans l'aide de PowerShell. – OldFart

+0

Je regarde l'exemple dans about_advanced_functions et je vois parens - où est l'échantillon dont vous parlez et qui n'a pas les parenthèses? – x0n

Questions connexes