2015-12-22 22 views
1

這是繼續到one question我在本網站上提到過。有兩個問題:在動態參數中使用ValidateSet

  1. 當我在腳本文件中的此代碼的最後一行下面添加任何函數時,我開始出現編譯錯誤。 (最後一行之後寫入的任何代碼/功能將顯示編譯錯誤)

-AND-

  • 我需要擴展參數以驗證添加在動態參數設定。當我嘗試下面的代碼時,添加的validate集適用於所有參數並給出錯誤。而我希望驗證集僅適用於「Workday」參數。

  • 此外,需要添加用於通過屬性名稱從管道從管道接受值,並且接受輸入以下參數(除了[Switch]參數)

  • 下面是代碼支持:

    [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

    如果某些參數需要單獨的屬性,那麼您必須爲它們創建單獨的'$ attributeCollection'。目前你只有一個。 – PetSerAl

    +0

    這確實有幫助!感謝您的投入。但第二個問題仍然存在。如果我在代碼的最後一行之後添加任何代碼,它會被認爲是錯誤:-(任何想法? – SavindraSingh

    +1

    Hi @SavindraSingh,對不起,在另一個線程上沒有回答,我已經離開了幾個星期了。不知道你是否已經解決了這個問題,但是你的問題與這個問題基本相同:http://stackoverflow.com/questions/30737491/how-do-i-define-functions-within-a-cmdletbinding-腳本還有一個他沒有提到的替代解決方案,你可以考慮將你的函數移動到一個單獨的文件中,然後你可以從主腳本中獲得點源文件 – Poorkenny

    回答

    1

    感謝@PetSerAl對他的建議和意見。當它們出現時,我們不能在Begin/Process/End塊之外放置代碼/函數。