Skip to content

Instantly share code, notes, and snippets.

@mattmcnabb
Last active August 29, 2015 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattmcnabb/d973f1ed11902d46b340 to your computer and use it in GitHub Desktop.
Save mattmcnabb/d973f1ed11902d46b340 to your computer and use it in GitHub Desktop.
Powershell Conditional Dynamic Parameters
function Test-ConditionalParameter
{
param
(
[validateset('Value1','Value2')]
[parameter()]
$Parameter1
)
dynamicparam
{
if ($PSBoundParameters.Parameter1 -eq 'Value1')
{
$Dictionary = New-Object -TypeName System.Management.Automation.RuntimeDefinedParameterDictionary
$ParamAttr = New-Object -TypeName System.Management.Automation.ParameterAttribute
$AttributeCollection = New-Object -TypeName 'Collections.ObjectModel.Collection[System.Attribute]'
$AttributeCollection.Add($ParamAttr)
$Parameter = New-Object -TypeName System.Management.Automation.RuntimeDefinedParameter -ArgumentList @('Parameter2', [string], $AttributeCollection)
$Dictionary.Add('Parameter2', $Parameter)
return $Dictionary
}
}
process {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment