我在一个函数中要求2个参数相互依赖,另一个参数与前2个参数互斥,有几个可选参数,然后2个参数互斥。
下面是我想要实现的参数原型。
function TestParms () {
[CmdletBinding()]
Param (
# $p1 and $p2 must be used together. Can be used with all other parameters except $p3
$p1,
$p2,
# $P3 cannot be used with $p1 and $p2. Can be used with all other parameters or alone.
$p3,
# independent parameters
$p4,
$p5,
$p6,
# $p7 and $p8 are exclusive to each other but can be used with other parameters, or alone
$p7,
$p8
)
write-host "The parameters work"
}
解释一下, $p1 和 $p2 必须一起使用。不能与 $p3 一起使用,并且可以在没有任何其他参数的情况下使用。 $p3 不能与 $p1 和 $p2 一起使用。可以单独使用或与其他参数一起使用。 $p4、$p5 和 $p5 是可选的。 $p6 和 $p7 是互斥的,但两者都可以与任何其他参数一起使用。 没有强制参数。
我尝试了多种参数集组合,但无法正常工作。 例如,如果我尝试让参数 $p3 与参数 $p7 和 $p8 一起使用,就像这样,它会失败。
Param (
# $p1 and $p2 must be used together. Can be used with all other parameters except $p3
$p1,
$p2,
# $P3 cannot be used with $p1 and $p2. Can be used with all other parameters or alone.
[Parameter(ParameterSetName='p3')]
[Parameter(ParameterSetName = 'p7.1')]
[Parameter(ParameterSetName = 'p8.1')]
$p3,
# independent parameters
$p4,
$p5,
$p6,
# $p7 and $p8 are exclusive to each other but can be used with other parameters, or alone
[Parameter(ParameterSetName = 'p7')]
[Parameter(ParameterSetName = 'p7.1')]
$p7,
[Parameter(ParameterSetName = 'p8')]
[Parameter(ParameterSetName = 'p8.1')]
$p8
)
Test-Parms -p3 '3'
导致参数设置错误,但是
Test-Parms -p3 '3' -p7 '7'
有效
以下套装可能会满足您的需求:
function TestParms {
[CmdletBinding(DefaultParameterSetName = 'independentParameters')]
Param (
[Parameter(ParameterSetName = 'p1p2p8', Mandatory)]
[Parameter(ParameterSetName = 'p1p2p7', Mandatory)]
[Parameter(ParameterSetName = 'p1p2', Mandatory)]
$p1,
[Parameter(ParameterSetName = 'p1p2p8', Mandatory)]
[Parameter(ParameterSetName = 'p1p2p7', Mandatory)]
[Parameter(ParameterSetName = 'p1p2', Mandatory)]
$p2,
[Parameter(ParameterSetName = 'p3p8', Mandatory)]
[Parameter(ParameterSetName = 'p3p7', Mandatory)]
[Parameter(ParameterSetName = 'p3', Mandatory)]
$p3,
$p4,
$p5,
$p6,
[Parameter(ParameterSetName = 'p3p7', Mandatory)]
[Parameter(ParameterSetName = 'p1p2p7', Mandatory)]
[Parameter(ParameterSetName = 'p7', Mandatory)]
$p7,
[Parameter(ParameterSetName = 'p3p8', Mandatory)]
[Parameter(ParameterSetName = 'p1p2p8', Mandatory)]
[Parameter(ParameterSetName = 'p8', Mandatory)]
$p8
)
Write-Host 'The parameters work'
}
测试:
# $p1 and $p2 must be used together. Can be used with all other parameters except $p3
TestParms -p1 a -p2 a # works
TestParms -p1 a -p2 a -p3 a # fails
TestParms -p1 a -p2 a -p8 a # works
TestParms -p1 a -p2 a -p7 a # works
TestParms -p1 a -p2 a -p7 a -p4 a # works
# $P3 cannot be used with $p1 and $p2. Can be used with all other parameters or alone.
TestParms -p3 a # works
TestParms -p3 a -p7 a # works
TestParms -p3 a -p7 a -p4 a # works
TestParms -p3 a -p8 a -p7 a # fails
# $p7 and $p8 are exclusive to each other but can be used with other parameters, or alone
TestParms -p7 a # works
TestParms -p8 a # works
TestParms -p7 a -p8 a # fails
TestParms -p7 a -p1 a -p2 a # works
# independent parameters
TestParms -p4 a # works
TestParms -p4 a -p5 a -p6 a # works
TestParms # works