Powershell开关参数接受参数



如何让$m switch接受一个参数,而$prebuild不需要任何参数

Param (
[Parameter(Mandatory=$true)]
[Switch]$m,
[Switch]$prebuild
)

脚本将执行如下,但我得到以下错误:

.test.ps1 -m java
A positional parameter cannot be found that accepts argument 'java'

$m改为[string]:

param(
[string]$m,
[switch]$prebuild
)

要测试一个参数是否被调用者传递,使用$PSBoundParameters自动变量:

if($PSBoundParameters.ContainsKey('m')){
# An argument was passed to `-m`
}

(如果您保留[Parameter(Mandatory=$true)]属性,则不需要最后一部分)

最新更新