Why does passing $null to a parameter with AllowNull() result in an error?

前端 未结 2 1181
情书的邮戳
情书的邮戳 2020-12-22 04:40

Consider the following code:

function Test
{
    [CmdletBinding()]
    param
    (
        [parameter(Mandatory=$true)]
        [AllowNull()]
        [String         


        
2条回答
  •  难免孤独
    2020-12-22 05:20

    You also need to add the [AllowEmptyString()] attribute if you plan on allowing nulls and empty strings.

    function Test
    {
        [CmdletBinding()]
        param
        (
            [parameter(Mandatory=$true)]
            [AllowNull()]
            [AllowEmptyString()]
            [String]
            $ComputerName
        ) 
        process{}
    }
    
    Test -ComputerName $null
    

提交回复
热议问题