1

This is in continuation to one question I asked earlier on this site. There are two issues:

  1. The moment I add any function below last line of this code in the script file, I start getting compilation error. (Any code/function written after last line will show compilation error)

-AND-

  1. I need to extend the parameters to add validate set in dynamic parameters. When I try below code, the validate set added is applying to all the parameters and giving error. Whereas, I want the validate set to apply only on 'Workday' parameter.

  2. Also, need to add support for accepting values from pipeline and accepting values from pipeline by property names to these parameters (except [Switch] parameters)

Here is the code:

[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
    }
}
4
  • 1
    If you need separate attributes for some parameters, then you have to create separate $attributeCollection for them. Currently you have only one. Commented Dec 22, 2015 at 22:25
  • That indeed helped!. Thanks for the inputs. However the second issues is still there. If I add any code after last line in this code, it is considered error :-(. Any thoughts? Commented Dec 23, 2015 at 9:12
  • 1
    Hi @SavindraSingh, sorry about not answering on the other thread, I've been away for a couple of weeks. I don't know if you already solved it, but the issue you have is basically the same as this question: stackoverflow.com/questions/30737491/… There's also an alternate solution that he doesn't mention. You can consider moving your functions to a separate file, which you then dot-source from the main script. Commented Jan 4, 2016 at 10:26
  • Thank you for the inputs. I have realized that I need to put all the functions in Begin block and rest (calling part) in process block. Commented Jan 7, 2016 at 15:19

1 Answer 1

1

Thanks to @PetSerAl for his suggestions and inputs. We can't place code/function outside of Begin/Process/End block when they are present.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.