C# Powershell cmdlet的嵌套参数
新手问题 如何为Powershell cmdlet声明嵌套参数?我想要一个类似这样的cmdletC# Powershell cmdlet的嵌套参数,c#,powershell,C#,Powershell,新手问题 如何为Powershell cmdlet声明嵌套参数?我想要一个类似这样的cmdlet New-FootballTeam [-Defenders [[-LeftBack] <string>] [[-RightBack] <string>] ] [-Midfielders [[-LeftWing] <string>] [[-RightWing] <string>] [[-CentreMidfield] <string>] ]
New-FootballTeam
[-Defenders [[-LeftBack] <string>] [[-RightBack] <string>] ]
[-Midfielders [[-LeftWing] <string>] [[-RightWing] <string>] [[-CentreMidfield] <string>] ]
New-FootballTeam
[[-LeftBack] <string>] [[-RightBack] <string>]
[[-LeftWing] <string>] [[-RightWing] <string>] [[-CentreMidfield] <string>]
提前感谢。PowerShell不支持您描述的参数嵌套。PowerShell支持多个参数集,允许您指定对特定命令调用有效的不同参数“集”。参数集(内部AllParametersSet除外-其中放置未分配参数集的参数)是互斥的。通常每个参数集都有一个唯一的参数,PowerShell使用该参数来选择用于参数解析的特定参数集-这是互斥位。如果PowerShell无法确定要使用的参数集,它将发出错误。在某些情况下,您可以通过使用
[CmdletBinding(DefaultParameterSet=“”)]
来帮助PowerShell,告诉PowerShell在无法根据提供的参数和参数确定参数集时要使用哪个参数集。有一种称为动态参数的简洁PowerShell功能,可用于解决此问题。不过有点冗长:
Function New-FootballTeam {
[CmdletBinding()]
Param(
[Switch]$Defenders,
[Switch]$Midfielders
)
DynamicParam {
$dynamicParameters = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary
$toAdd = @()
if ($Defenders) {
$toAdd += @('LeftBack', 'RightBack')
}
if ($Midfielders) {
$toAdd += @('LeftWing', 'RightWing')
}
$toAdd | % {
$attr = New-Object System.Management.Automation.ParameterAttribute
$attr.ParameterSetName = "__AllParameterSets"
$attr.Mandatory = $True
$attributes = New-Object System.Collections.ObjectModel.Collection[System.Attribute]
$attributes.Add($attr)
$param = New-Object System.Management.Automation.RuntimeDefinedParameter(
$_,
'string',
$attributes
)
$dynamicParameters.Add($_, $param)
}
return $dynamicParameters
}
Begin {
$LeftBack = $PSBoundParameters.LeftBack
$RightBack = $PSBoundParameters.RightBack
$LeftWing = $PSBoundParameters.LeftWing
$RightWing = $PSBoundParameters.RightWing
}
End {
if ($Defenders) {
Write-Host "LeftBack : $LeftBack"
Write-Host "RightBack: $RightBack"
}
if ($Midfielders) {
Write-Host "LeftWing : $LeftWing"
Write-Host "RightWing: $RightWing"
}
}
}
但是,此解决方案有一些警告-使用“获取帮助”时不会列出动态参数,Intellisense有时也会使用这些参数。但它确实起作用,并且由于示例中的参数设置为强制,当设置后卫/中场开关时,PS将提示您输入所需的边路/后卫。
这样定义参数:
function New-FootballTeam {
param(
[Parameter(Mandatory = $true, ParameterSetName = "Defenders")]
[switch]$Defenders,
[Parameter(ParameterSetName = "Defenders", Position = 0)]
[string]$LeftBack,
[Parameter(ParameterSetName = "Defenders", Position = 1)]
[string]$RightBack,
[Parameter(Mandatory = $true, ParameterSetName = "Midfielders")]
[switch]$Midfielders,
[Parameter(ParameterSetName = "Midfielders", Position = 0)]
[string]$LeftWing,
[Parameter(ParameterSetName = "Midfielders", Position = 1)]
[string]$RightWing,
[Parameter(ParameterSetName = "Midfielders", Position = 2)]
[string]$CentreMidfield
)
}
会给你想要的东西:
NAME
New-FootballTeam
SYNTAX
New-FootballTeam [[-LeftBack] <string>] [[-RightBack] <string>] -Defenders [<CommonParameters>]
New-FootballTeam [[-LeftWing] <string>] [[-RightWing] <string>] [[-CentreMidfield] <string>] -Midfielders [<CommonParameters>]
名称
新足球队
语法
新足球队[[-左后卫][-右后卫]]-后卫[]
新足球队[[-左翼][-右翼][[-中卫]]-中场[]
虽然我不太确定这个例子中的参数集或者后卫和中场的参数。您可以准确地说出参数名称的含义。将所有位置作为参数放在一个集合中会更简单。PowerShell中的参数集是互斥的(尽管某些参数可以在所有参数集中)。这是你的意图吗?不能同时指定-LeftBack和-LeftWing?如果不是这样的话,为什么你需要这些参数——后卫和中场?您可以有-LeftBackDefender、-RightBackDefender、-LeftWingMidderd等等。我的目的只是实现一个类似于我上面提到的cmdlet。在这里发布之前,我一直在寻找方法,遇到了参数集,并在这里写下了相关内容,因为我不知道这是否是解决方案。是的。有一些明显的解决方法,比如使用-LeftBackDefender和-RightBackDefender。但肯定有一种方法可以像我在问题中提到的那样实现它?那是行不通的,因为OP不希望这些参数相互排斥,而且参数集是相互排斥的。@KeithHill根据问题的格式,我可能误解了OP想要什么。我想在需要的地方设置两个参数。我不知道你说的“相互排斥”是什么意思。当然,当您调用命令时,它必须解析为单个明确的参数集,但参数可以位于一个、所有或选择参数集中。是的,但您的定义不允许为单个调用指定卫士和中场。虽然你可以把五个“嵌套”参数放在两个参数集中,但顶级参数集(后卫和中场)仍然是互斥的,所以这两个参数不能出现在同一调用中。是c代码吗?这不是我如何在c上重写它?