47

Please try this:

function f1
{
    param(
    [Parameter(Mandatory=$true,ValueFromPipelineByPropertyName=$true)]
    [string]
    $Text
    )
    $text
}

function f2
{
    param(
    [Parameter(Mandatory=$true,ValueFromPipelineByPropertyName=$true)]
    #[string]
    $Text
    )
    $text
}

function f3
{
    param(
    [Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true)]
    [string]
    $Text
    )
    $text
}

f1 ''
f2 ''
f3 ''

Here f1 throws an error. Now try

f2 $null 
f3 $null    

This time only f2 throws an error. What I want is a function f, so that

f '' # is accepted
f $null # returns an error
2
  • Sorry, I don't understand, your function 'f2' works as you want. f2 '' is accepted and f2 $null generate an error.
    – JPBlanc
    Commented Jun 19, 2011 at 16:27
  • @JPB I think the problem there is about the data type. Commented Jun 19, 2011 at 16:28

3 Answers 3

93

The Mandatory attribute blocks null and empty values and prompts you for a value. To allow empty values (including null) add the AllowEmptyString parameter attribute:

function f1
{
    param(
    [Parameter(Mandatory=$true,ValueFromPipelineByPropertyName=$true)]
    [AllowEmptyString()]
    [string]$Text
    )
    $text
}
3
  • Your version is better than that of Roman Kuzmin, it rejects non strings like f1 @(1, 2). The fact that f1 $null is accepted seems to be the slightest problem. BTW this nearly identical to f3.
    – bernd_k
    Commented Jun 20, 2011 at 10:11
  • Try to replace AllowEmptyString with ValidateNotNull. it allows empty strings but not nulls.
    – Shay Levy
    Commented Jun 20, 2011 at 14:57
  • No, ValidateNotNull() fails for f1 ''.
    – bernd_k
    Commented Jun 21, 2011 at 5:55
7

Here is the solution that fits the requirements with a caveat.

function f1
{
    param(
    [Parameter(Mandatory=$true,ValueFromPipelineByPropertyName=$true)]
    $Text
    )
    Write-Host 'Working'
    $text
}

f1 ''
f1 $null

Output:

Working
f1 : Cannot bind argument to parameter 'Text' because it is null.

Caveat

In order to fit the requirements we have to omit the explicit type declaration of [string]. The problem is that PowerShell tends to convert nulls to empty strings everywhere where the [string] type is specified. Thus, if we use the type declaration then null value actually never comes to the function.

P.S. Here is a related issue submitted: It isn't possible to pass null as null into a .NET method that has a parameter of type String

2
  • I have just realized that this is exactly the function f2. So, that is not quite a real answer, perhaps :) Commented Jun 19, 2011 at 17:04
  • 2
    +1 That what I'am saying from the begining ;o) But you give the explanation, you are the guy.
    – JPBlanc
    Commented Jun 19, 2011 at 17:06
1

Just for the sake of completeness, if you want your input be validated against string type, you can do it afterward parameters declaration:

function f1
{
    param(
    [Parameter(Mandatory=$true,ValueFromPipelineByPropertyName=$true)]
    $Text
    )
    if (!($text -eq '') -and !($text -as [string])) {write-host "wrong type"; return }
    $text
}

This function behaves as follows:

  • Throws "Cannot bind argument.." exception when input is $null
  • Passes when input is an empty string
  • Exits with message wrong type when input is not a string

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.