【发布时间】:2018-01-23 07:36:21
【问题描述】:
测试代码(test.ps1):
Param(
[string]$content = $null,
[switch]$flag = $false
)
Write-Host $content
输出:
PS C:\Users\powershell> .\test.ps1 PS C:\Users\powershell> .\test.ps1 -flag $true 真的 PS C:\Users\powershell> .\test.ps1 -flag $false 错误的 PS C:\Users\powershell> .\test.ps1 -flag $true -content "lalala" 拉拉拉无论我将$content设置为$null,还是"",或者没有任何默认值,输出都是一样的。
那么为什么$content 会从$flag 中取值呢?
【问题讨论】:
-
你没有向
-flag传递任何值。 -
开关参数,在它们的一般用法中,如果你不在命令行中指定它们,则为 $false,如果你在命令行中指定它们,则为 $true。 @PetSerAl
-
我无法重现此问题,您使用的是什么 psversion?什么操作系统?
-
我认为这里真正的混淆点是
$true被解析器解释为位置参数并隐式绑定到-Content
标签: powershell parameter-passing