如何解决Powershell-参数集
param (
[Parameter (Mandatory = $false,ParameterSetName = "SendMail",HelpMessage = "Call this parameter to send information mail ONLY.")]
[switch]$SendMail,[Parameter (Mandatory = $false,Position = 0,ParameterSetName = "UseRegion",HelpMessage = "Call this parameter to use the function with Regions.")]
[Parameter (Mandatory = $false,ParameterSetName = "UseCountry")]
[switch]$UseRegions,ParameterSetName = "SendMail")]
[Parameter (Mandatory = $true,HelpMessage = "Define the region to apply the function.")]
[validateset("AMER","APAC","EMEA")]
[string]$Region,ParameterSetName = "SendMail")]
[Parameter (Mandatory = $false,ParameterSetName = "UseRegion")]
[Parameter (Mandatory = $true,HelpMessage = "Define the country to apply the function.",ParameterSetName = "UseCountry")]
[ValidateNotNullOrEmpty()]
[string]$Country,HelpMessage = "Define months for a machine to be outdated. Default value is 3.")]
[Validaterange(1,[int]::MaxValue)]
[int]$Months = 3,HelpMessage = "Define days for a machine to be on the ""disabled"" OU before permanent deletion. Minimum and default value is 7.")]
[Validaterange([int] 7,[int] 30)]
[int]$DaysToDeletion = 7,HelpMessage = "Define operation to exclude from the rule. I.E.: Operations that does not connect to the VPN.")]
[string]$Exclude
)
我正在尝试完成以下任务:
当我呼叫-SendMail NO参数为必填项时 当我打电话给-UseRegions时,-Region是强制性的,其余的则不是。 当我不致电-UseRegions时,-Country是强制性的,其余则不是。
我一直在使用参数集,但是我永远无法解决它。 我也不想有一个名为-UseCountry的额外参数,我想让-UseRegions同时控制-Region和-Country。
有人喜欢吗?
非常感谢!!!
解决方法
我明白了,参数集有时甚至会使我的头旋转。听起来,您想要3套不同的套。
- 国家
- 地区
- 发送邮件
您可以使用-SendMail
除去mandatory = $true
开关以外的所有开关。
function potato
{
param (
[Parameter (
Mandatory = $true,ParameterSetName = "SendMail",HelpMessage = "Call this parameter to send information mail ONLY."
)]
[Parameter (
Mandatory = $false,ParameterSetName = "UseRegion"
)]
[Parameter (
Mandatory = $false,ParameterSetName = "UseCountry"
)]
[switch]
$SendMail,[Parameter (
Mandatory = $false,ParameterSetName = "SendMail"
)]
[Parameter (
Mandatory = $true,ParameterSetName = "UseRegion",HelpMessage = "Define the region to apply the function."
)]
[Parameter (
Mandatory = $false,ParameterSetName = "UseCountry"
)]
[ValidateSet("AMER","APAC","EMEA")]
[string]
$Region,ParameterSetName = "SendMail"
)]
[Parameter (
Mandatory = $false,ParameterSetName = "UseRegion"
)]
[Parameter (
Mandatory = $true,HelpMessage = "Define the country to apply the function.",ParameterSetName = "UseCountry"
)]
[ValidateNotNullOrEmpty()]
[string]
$Country,ParameterSetName = "UseCountry"
)]
[Parameter (
Mandatory = $false,HelpMessage = "Define months for a machine to be outdated. Default value is 3."
)]
[ValidateRange(1,[int]::MaxValue)]
[int]
$Months = 3,HelpMessage = "Define days for a machine to be on the ""Disabled"" OU before permanent deletion. Minimum and default value is 7."
)]
[ValidateRange([int] 7,[int] 30)]
[int]
$DaysToDeletion = 7,HelpMessage = "Define operation to exclude from the rule. I.E.: Operations that does not connect to the VPN."
)]
[string]
$Exclude
)
}
现在,如果您执行help potato
,则SYNTAX部分将显示此内容
potato -Country <string> [-SendMail] [-Region {AMER | APAC | EMEA}] [-Months <int>] [-
DaysToDeletion <int>] [-Exclude <string>] [<CommonParameters>]
potato -Region {AMER | APAC | EMEA} [-SendMail] [-Country <string>] [-Months <int>] [-
DaysToDeletion <int>] [-Exclude <string>] [<CommonParameters>]
potato -SendMail [-Region {AMER | APAC | EMEA}] [-Country <string>] [-Months <int>] [-
DaysToDeletion <int>] [-Exclude <string>] [<CommonParameters>]
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。