Skip to content

Instantly share code, notes, and snippets.

@LindnerBrewery
Created July 18, 2022 12:49
Show Gist options
  • Save LindnerBrewery/5ddc9594c87a51e4b56f89f44065a3f7 to your computer and use it in GitHub Desktop.
Save LindnerBrewery/5ddc9594c87a51e4b56f89f44065a3f7 to your computer and use it in GitHub Desktop.
Test-DynamicParameters
function Test-DynamicParameters {
# CmdletBinding is mandatory for using dynamic parameters
[CmdletBinding(DefaultParameterSetName = "default")]
param (
# One DynamicParam will only be usable if $name contains at least 2 elements
[Parameter(mandatory = $false)]
[ValidateNotNullOrEmpty()]
[String[]]
$Name
)
DynamicParam {
# Create a dictionary that holds all dynamic parameters
$paramDictionary = [System.Management.Automation.RuntimeDefinedParameterDictionary]::new()
# create a parameter attribute that contains the properties like help, mandatory, position etc. https://bit.ly/3RGsZI6
$paramAttributes = [System.Management.Automation.ParameterAttribute]::new()
$paramAttributes.Mandatory = $false
$paramAttributes.HelpMessage = "This is the help for DynParam"
$paramAttributes.ParameterSetName = "__AllParameterSets"
# create an attributecollection and add attributes to the list
$attributeCollection = [System.Collections.ObjectModel.Collection[System.Attribute]]::new()
$attributeCollection.Add($paramAttributes)
# declare the Parameter and add of to the dictionary of dynamic parameters
$parameterName = "DynParam"
$parameterDefinition = [System.Management.Automation.RuntimeDefinedParameter]::new( $parameterName, [String], $attributeCollection)
$paramDictionary.Add($parameterName, $parameterDefinition)
# create a second dynamic parameter depending on the value of an other parameter. In this case if the parameter name hast more than 2 elements
# conditional dynamic parameters can't be found on the help nor can you find the by tabbing through the parameters!!!!!!!
if ($Name.length -gt 1) {
# create a parameter attribute that contains the properties like help, mandatory, position etc. https://bit.ly/3RGsZI6
$paramAttributes = [System.Management.Automation.ParameterAttribute]::new()
$paramAttributes.Mandatory = $true
# create an attributecollection and add attributes to the list
$attributeCollection = [System.Collections.ObjectModel.Collection[System.Attribute]]::new()
$attributeCollection.Add($paramAttributes)
# declare the Parameter and add of to the dictionary of dynamic parameters
$parameterName = "ConditionalDynParam"
$parameterDefinition = [System.Management.Automation.RuntimeDefinedParameter]::new( $parameterName, [switch], $attributeCollection)
$paramDictionary.Add($parameterName, $parameterDefinition)
}
# publish the parameter
return $paramDictionary
}
# when using dynamic parameter you need a Process block
Process {
# dynamic parameters can't be accessed directly.
# dynamic parameters can only be accessed through $PSBoundParameters
$dynParam = $PSBoundParameters.DynParam
$conditionalDynParam = $PSBoundParameters.ConditionalDynParam
if ($dynParam) {
Write-Host "dynParam has the value: $dynParam"
}
if ($conditionalDynParam) {
Write-Host "Name Array = $Name"
Write-Host "conditional dynamic param is $conditionalDynParam"
}
}
}
<#
PS > Test-DynamicParameters -Name "foo","bar" -DynParam 'param' -ConditionalDynParam
dynParam has the value: param
Name Array = foo bar
conditional dynamic param is True
PS > Test-DynamicParameters -Name "foo" -ConditionalDynParam
Test-DynamicParameters: A parameter cannot be found that matches parameter name 'ConditionalDynParam'.
This fails because the condition is not met
PS > Test-DynamicParameters -DynParam 'param'
dynParam has the value: param
#>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment