Reputation: 961
This is in continuation to one question I asked earlier on this site. There are two issues:
-AND-
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.
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
}
}
Upvotes: 1
Views: 758
Reputation: 961
Thanks to @PetSerAl for his suggestions and inputs. We can't place code/function outside of Begin/Process/End block when they are present.
Upvotes: 1