How to validate PowerShell Function Parameters allowing empty strings?

Viewed 42928

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
3 Answers
Related