在动态参数

时间:2015-12-22 14:36:38

标签: validation powershell

这是我前面在本网站上提到的one question的延续。有两个问题:

  1. 当我在脚本文件中添加此代码的最后一行下面的任何函数时,我开始收到编译错误。 (在最后一行之后写的任何代码/函数将显示编译错误)
  2. - 和 -

    1. 我需要扩展参数以在动态参数中添加验证集。当我尝试下面的代码时,添加的验证集将应用于所有参数并给出错误。鉴于,我希望验证集仅适用于'Workday'参数。

    2. 此外,需要添加对从管道接受值以及通过属性名称从管道接受值到这些参数([Switch]参数除外)的支持

    3. 以下是代码:

      [CmdletBinding(DefaultParameterSetName='DefaultConfiguration')]
      Param(
          [Parameter(Mandatory=$True, ValueFromPipeline=$True, ValueFromPipelineByPropertyName=$True)]
          [String]$ResourceGroupName,
          [Parameter(Mandatory=$True, ValueFromPipeline=$True, ValueFromPipelineByPropertyName=$True)]
          [String]$VaultName,
      
          [Parameter(Mandatory=$True, ValueFromPipeline=$True, ValueFromPipelineByPropertyName=$True)]
          [String]$SubscriptionID,
      
          [Parameter(Mandatory=$True, ValueFromPipeline=$True, ValueFromPipelineByPropertyName=$True)]
          [String]$Location,
      
          [Switch]$CustomizeDPMSubscriptionSettings,
          [Switch]$SetEncryption,
          [Switch]$SetProxy,
          [Switch]$SetThrottling
      )
      
      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 "-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)
      
              # 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", [Int32], $attributeCollection)   
                  $paramDictionary.Add("ProxyServerPort", $dynParam2)
              }
              if ($SetThrottling) {
                  $dynParam1 = New-Object -Type System.Management.Automation.RuntimeDefinedParameter("ThrottlingStartWorkHour", [Int32], $attributeCollection)   
                  $paramDictionary.Add("ThrottlingStartWorkHour", $dynParam1)
                  $dynParam2 = New-Object -Type System.Management.Automation.RuntimeDefinedParameter("ThrottlingEndWorkHour", [Int32], $attributeCollection)   
                  $paramDictionary.Add("ThrottlingEndWorkHour", $dynParam2)
                  $dynParam3 = New-Object -Type System.Management.Automation.RuntimeDefinedParameter("ThrottlingWorkHourBandwidth", [Long], $attributeCollection)   
                  $paramDictionary.Add("ThrottlingWorkHourBandwidth", $dynParam3)
                  $dynParam4 = New-Object -Type System.Management.Automation.RuntimeDefinedParameter("ThrottlingNonWorkHourBandwidth", [Long], $attributeCollection)   
                  $paramDictionary.Add("ThrottlingNonWorkHourBandwidth", $dynParam4)
      
                  $_Days = @("Su","Mo","Tu","We","Th","Fr","Sa")
                  $ValidateSet = New-Object System.Management.Automation.ValidateSetAttribute($_Days)
                  $dynParam5 = New-Object -Type System.Management.Automation.RuntimeDefinedParameter("Workday", [System.DayofWeek], $attributeCollection)
                  $dynParam5.Attributes.Add($ValidateSet) 
                  $paramDictionary.Add("Workday", $dynParam5)
              }
          }
          return $paramDictionary
      }
      
      Process {
          foreach ($key in $PSBoundParameters.keys) {
              Set-Variable -Name $key -Value $PSBoundParameters."$key" -Scope 0
          }
      }
      

1 个答案:

答案 0 :(得分:1)

感谢@PetSerAl的建议和意见。我们不能将代码/函数置于Begin / Process / End块之外。

相关问题