根据开关强制参数 - PowerShell

时间:2015-12-18 10:17:04

标签: powershell

如果使用交换机或交换机组合,我需要强制执行一些参数。以下是我想要做的例子:

[CmdletBinding(DefaultParameterSetName='DefaultConfiguration')]
Param
(        
[Parameter(Mandatory=$true)][String]$Location,
[Parameter(Mandatory=$true)][String]$DPMServername,

[Parameter(Mandatory=$False, ParameterSetName='CustomConfiguration')]
[Switch]$CustomizeDPMSubscriptionSettings,
[Parameter(Mandatory=$True, ParameterSetName='CustomConfiguration')]
[String]$StagingAreaPath,

[Parameter(Mandatory=$False, ParameterSetName='EncryptionSettings')]
[Parameter(ParameterSetName='CustomConfiguration')]
[Switch]$SetEncryption,
[Parameter(Mandatory=$true, ParameterSetName='EncryptionSettings')]
[Parameter(Mandatory=$False, ParameterSetName='CustomConfiguration')]
[String]$EncryptionPassPhrase,

[Parameter(Mandatory=$False, ParameterSetName='ProxyEnabled')]
[Parameter(ParameterSetName='CustomConfiguration')]
[Switch]$SetProxy,
[Parameter(Mandatory=$true, ParameterSetName='ProxyEnabled')]
[Parameter(ParameterSetName='CustomConfiguration')]
[String]$ProxyServerAddress,
[Parameter(Mandatory=$true, ParameterSetName='ProxyEnabled')]
[Parameter(Mandatory=$False, ParameterSetName='CustomConfiguration')]
[String]$ProxyServerPort
)

在这里,我需要遵循以下条件:

  1. 如果使用-CustomizeDPMSubscriptionSettings(Switch)参数, 它必须要求Staging AreaPath ----这是正常工作
  2. 仅当-CustomizeDPMSubscriptionSettings(Switch)参数为 与-SetEncryption一起使用时,它必须要求-EncryptionPassPhrase
  3. 仅当-CustomizeDPMSubscriptionSettings(Switch)参数为 与-SetProxy一起使用时,它必须要求-ProxyServerAddress和-ProxyServerPort
  4. 很抱歉,如果这听起来像是一个重复的问题,但我在这里发现的其他帖子并没有帮助我解决我的问题。我很困惑: - (

    注意:上面的代码是我尝试使用不同组合的一部分。请根据需要更正。

1 个答案:

答案 0 :(得分:3)

这是 解决方案,它似乎可以满足您的期望。
我所做的是为每个可能的组合创建一个参数集。
- CustomConfiguration
- 加密设置
- ProxyEnabled
- EncryptionAndProxy


一个限制是除非使用 EncryptionAndProxy ,否则它不会提示特定的缺失参数,而是会声明它无法解析参数集。

[CmdletBinding(DefaultParameterSetName='DefaultConfiguration')]
Param
(        
[Parameter(Mandatory=$true)][String]$Location,
[Parameter(Mandatory=$true)][String]$DPMServername,

[Parameter(Mandatory=$True, ParameterSetName='CustomConfiguration')]
[Parameter(Mandatory=$True, ParameterSetName='EncryptionSettings')]
[Parameter(Mandatory=$True, ParameterSetName='ProxyEnabled')]
[Parameter(Mandatory=$True, ParameterSetName='EncryptionAndProxy')]
[Switch]$CustomizeDPMSubscriptionSettings,

[Parameter(Mandatory=$True, ParameterSetName='CustomConfiguration')]
[Parameter(Mandatory=$True, ParameterSetName='EncryptionSettings')]
[Parameter(Mandatory=$True, ParameterSetName='ProxyEnabled')]
[Parameter(Mandatory=$True, ParameterSetName='EncryptionAndProxy')]
[String]$StagingAreaPath,

[Parameter(Mandatory=$True, ParameterSetName='EncryptionSettings')]
[Parameter(Mandatory=$True, ParameterSetName='EncryptionAndProxy')]
[Switch]$SetEncryption,

[Parameter(Mandatory=$true, ParameterSetName='EncryptionSettings')]
[Parameter(Mandatory=$True, ParameterSetName='EncryptionAndProxy')]
[String]$EncryptionPassPhrase,

[Parameter(Mandatory=$True, ParameterSetName='ProxyEnabled')]
[Parameter(Mandatory=$True, ParameterSetName='EncryptionAndProxy')]
[Switch]$SetProxy,

[Parameter(Mandatory=$true, ParameterSetName='ProxyEnabled')]
[Parameter(Mandatory=$True, ParameterSetName='EncryptionAndProxy')]
[String]$ProxyServerAddress,

[Parameter(Mandatory=$true, ParameterSetName='ProxyEnabled')]
[Parameter(Mandatory=$True, ParameterSetName='EncryptionAndProxy')]
[String]$ProxyServerPort
)

我正在研究基于动态参数的第二种潜在解决方案。

编辑:正如所承诺的,这是一个基于动态参数的解决方案

[CmdletBinding(DefaultParameterSetName='DefaultConfiguration')]
Param
(        
    [Parameter(Mandatory=$true)][String]$Location,
    [Parameter(Mandatory=$true)][String]$DPMServername,

    [Switch]$CustomizeDPMSubscriptionSettings,
    [Switch]$SetEncryption,
    [Switch]$SetProxy
)

DynamicParam
{
    $paramDictionary = New-Object -Type System.Management.Automation.RuntimeDefinedParameterDictionary
    $attributes = New-Object System.Management.Automation.ParameterAttribute
    $attributes.ParameterSetName = "__AllParameterSets"
    $attributes.Mandatory = $true
    $attributeCollection = New-Object -Type System.Collections.ObjectModel.Collection[System.Attribute]
    $attributeCollection.Add($attributes)
    # If "-SetEncryption" is used, then add the "EncryptionPassPhrase" parameter
    if($SetEncryption)
    { 
        $dynParam1 = New-Object -Type System.Management.Automation.RuntimeDefinedParameter("EncryptionPassPhrase", [String], $attributeCollection)   
        $paramDictionary.Add("EncryptionPassPhrase", $dynParam1)
    }
    # If "-SetProxy" is used, then add the "ProxyServerAddress" "ProxyServerPort" and parameters
    if($SetProxy)
    {
        $dynParam1 = New-Object -Type System.Management.Automation.RuntimeDefinedParameter("ProxyServerAddress", [String], $attributeCollection)   
        $paramDictionary.Add("ProxyServerAddress", $dynParam1)
        $dynParam2 = New-Object -Type System.Management.Automation.RuntimeDefinedParameter("ProxyServerPort", [String], $attributeCollection)   
        $paramDictionary.Add("ProxyServerPort", $dynParam2)
    }
    # If "-CustomizeDPMSubscriptionSettings" is used, then add the "StagingAreaPath" parameter
    if($CustomizeDPMSubscriptionSettings)
    {
        $dynParam1 = New-Object -Type System.Management.Automation.RuntimeDefinedParameter("StagingAreaPath", [String], $attributeCollection)   
        $paramDictionary.Add("StagingAreaPath", $dynParam1)
    }
    return $paramDictionary
}
Process{
    foreach($key in $PSBoundParameters.keys)
    {
        Set-Variable -Name $key -Value $PSBoundParameters."$key" -Scope 0
    }
}

这个功能是根据每个开关的存在动态地为您的功能添加参数。
这支持自动完成,并且对缺少的参数有更好的支持。如果使用相应的开关,它将明确询问缺少的参数。
第二次编辑:我添加了Process部分,该部分是强制性的,以及变量创建位,这使事情变得更加容易。