Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
基于开关-PowerShell将参数设为强制参数_Powershell - Fatal编程技术网

基于开关-PowerShell将参数设为强制参数

基于开关-PowerShell将参数设为强制参数,powershell,Powershell,我当时的处境是,只有在使用开关或开关组合的情况下,我才需要强制设置某些参数。下面是我正在尝试做的示例: [CmdletBinding(DefaultParameterSetName='DefaultConfiguration')] Param ( [Parameter(Mandatory=$true)][String]$Location, [Parameter(Mandatory=$true)][String]$DPMServername, [Parameter(Mandato

我当时的处境是,只有在使用开关或开关组合的情况下,我才需要强制设置某些参数。下面是我正在尝试做的示例:

[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
)
在这里,我需要遵循以下条件:

  • 如果使用-CustomizeDPMSubscriptionSettings(开关)参数, 它必须请求暂存区域路径——这工作正常
  • 仅当-CustomizeDPMSubscriptionSettings(开关)参数为 与-SetEncryption一起使用时,它必须请求-EncryptionPassPhrase
  • 并且仅当-CustomizeDPMSubscriptionSettings(开关)参数为 与-SetProxy一起使用时,它必须请求-ProxyServerAddress和-ProxyServerPort
  • 抱歉,如果这听起来像是一个重复的问题,但我在这里找到的其他帖子并没有帮助我解决我的问题。我很困惑:-(

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

    这里有一个解决方案,似乎可以满足您的期望。
    我所做的是为每个可能的组合创建一个参数集。
    -自定义配置
    -加密设置
    -代理启用
    -加密和代理


    一个限制是,除非使用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
        }
    }
    
    这个函数所做的是根据每个开关的存在动态地向函数添加参数。
    它支持自动完成,并且更好地支持缺少的参数。如果使用相应的开关,它将显式地请求缺少的参数。

    第二次编辑:我添加了此构造必需的
    过程
    部分,以及变量创建位,这使事情变得更容易。

    可能的组合是什么?可以同时使用-SetEncryption和-SetProxy吗?是的,我可以。但即使在这种情况下,也应该使用-CustomizeDPMSubscriptionSettingss、 所有可能的组合都是:-CustomizeDPMSubscriptionSettings-SetEncryption,-CustomizeDPMSubscriptionSettings-SetProxy和-CustomizeDPMSubscriptionSettings-SetEncryption-SetProxy。当使用
    -CustomizeDPMSubscriptionSettings-SetEncryption
    组合时,这里的要点是必须要求所有与加密相关的参数。代理相关参数也是如此。感谢您为编写此代码付出了如此多的努力。不过,我明天到达办公室后将尝试此代码,并让您知道哪一个更适合我。第二个解决方案效果非常好。you rock@Poorkenny。我感谢您在这里所做的努力和投入。非常感谢,广告中的圣诞快乐万斯:-)我很抱歉在接受这个答案后添加评论,但是,在我单独使用这个之前,它工作得很好。但当我将其粘贴到最终脚本中时,它开始出现编译错误。你能帮我消除编译错误吗?如何将完整的脚本文件作为附件共享?编辑:例如,若我在代码的最后一行之后添加任何函数,它将无法识别该函数。我不想把这整件事放在一个函数中,因为这个脚本文件中有很多函数,我必须从脚本范围调用它们。我问了另外一些问题,并添加了一些问题。请参阅同一页