当你这样做时:
Move-ActiveMailboxDatabase -Confirm $false
你不是说Confirm
参数接受$false
。你是说-Confirm
并且还将一个(单独的)参数传递给具有值的 cmdlet$false
.
Since Confirm
是一个开关,只是存在-Confirm
意味着这是真的。不存在-Confirm
意味着它是假的。
让我给你一个脚本示例:
param([switch]$test)
write-host Test is $test
如果您只是运行没有任何参数的脚本,即.\script.ps1
你得到输出:
Test is False
如果你运行它作为.\script.ps1 -test
,输出是
Test is True
如果你运行它作为.\script.ps1 -test $false
,输出是
Test is True
如果你运行它作为.\script.ps1 -test:$false
输出是
Test is False
在开关变量本身的值必须从另一个变量确定的情况下,:
用来。
例如,考虑以下脚本:
param ([boolean]$in)
function func([switch] $test){
write-host Test is $test
}
func -test:$in
在这里,如果您将其运行为.\script.ps1 -in $false
, 你得到
Test is false
如果您无法使用:
,你必须将其写为:
if($in){ func -test}
else { func }