Consider the following code:
function Test
{
[CmdletBinding()]
param
(
[parameter(Mandatory=$true)]
[AllowNull()]
[String]
$ComputerName
)
process{}
}
Test -ComputerName $null
Based on the official documentation for AllowNull I was expecting that $ComputerName could either be [string] or $null. However, running the above code results in the following error:
[14,24: Test] Cannot bind argument to parameter 'ComputerName' because it is an empty string.
Why doesn't passing $null for $ComputerName work in this case?
$nullis cast to an empty string, due to the parameter type, and thus isn't covered by the validation attribute anymore. UsingAllowEmptyString()instead works. I'd consider this a documentation bug.Test -ComputerNamewith no value specified? Why not?