Skip to content

Instantly share code, notes, and snippets.

@dlwyatt
Created September 4, 2014 12:08
Show Gist options
  • Save dlwyatt/dd640a732af5696136f9 to your computer and use it in GitHub Desktop.
Save dlwyatt/dd640a732af5696136f9 to your computer and use it in GitHub Desktop.
DynamicParam class example
Add-Type -TypeDefinition @'
using System;
using System.Management.Automation;
public class DynamicParamExample
{
[Parameter()]
[ValidateSet("one", "two", "three")]
public string MyDynamicParameter { get; set; }
}
'@
function Test-Function
{
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[string] $MyStaticParameter
)
dynamicparam
{
New-Object DynamicParamExample
}
end
{
$PSBoundParameters
}
}
Get-Command Test-Function -Syntax
Test-Function -MyStaticParameter Static -MyDynamicParameter three
@dlwyatt
Copy link
Author

dlwyatt commented Sep 4, 2014

Note: The dynamicparam block could have logic that chooses between which class to return, etc. This is how Get-ChildItem and other similar cmdlets work, rather than building a RuntimeDefinedParameterDictionary.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment