【发布时间】:2022-02-09 07:45:02
【问题描述】:
考虑以下代码:
function Test
{
[CmdletBinding()]
param
(
[parameter(Mandatory=$true)]
[AllowNull()]
[String]
$ComputerName
)
process{}
}
Test -ComputerName $null
基于the official documentation for AllowNull,我期望$ComputerName 可以是[string] 或$null。但是,运行上述代码会导致以下错误:
[14,24: Test] 无法将参数绑定到参数“ComputerName”,因为它是空的 字符串。
为什么在这种情况下为 $ComputerName 传递 $null 不起作用?
【问题讨论】:
-
我不能给你一个明确的解释,但在我看来
$null被转换为一个空字符串,由于参数类型,因此不包含在验证属性中了。使用AllowEmptyString()代替工作。我认为这是一个文档错误。
标签: powershell parameters nullable