Created
September 4, 2014 12:08
-
-
Save dlwyatt/dd640a732af5696136f9 to your computer and use it in GitHub Desktop.
DynamicParam class example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.