Last active
August 11, 2023 22:46
-
-
Save indented-automation/979ede510a695e9b0e91c67ef101c84e to your computer and use it in GitHub Desktop.
New-DynamicParameter
This file contains hidden or 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
function New-DynamicParameter { | |
<# | |
.SYNOPSIS | |
Create a new dynamic parameter object for use with a dynamicparam block. | |
.DESCRIPTION | |
New-DynamicParameter allows simplified creation of runtime (dynamic) parameters. | |
.EXAMPLE | |
New-DynamicParameter Name -DefaultValue "Test" -ParameterType "String" -Mandatory -ValidateSet "Test", "Live" | |
.EXAMPLE | |
New-DynamicParameter Name -ValueFromPipelineByPropertyName | |
.EXAMPLE | |
New-DynamicParameter Name -ValidateRange 1, 2 | |
.NOTES | |
Change log: | |
24/10/2014 - Chris Dent - Added support for ValidatePattern options and ValidateSet case sensitivity. | |
22/10/2014 - Chris Dent - First release. | |
#> | |
[CmdletBinding()] | |
[OutputType([System.Management.Automation.RuntimeDefinedParameter])] | |
param ( | |
# The name of the parameter to create. | |
[Parameter(Mandatory = $true)] | |
[Alias('Name')] | |
[String]$ParameterName, | |
[Object]$DefaultValue, | |
# The .NET type of this parameter. | |
[Type]$ParameterType = "Object", | |
# Set the mandatory flag for this parameter. | |
[Switch]$Mandatory, | |
# Define a position for the parameter. | |
[Int32]$Position = -2147483648, | |
# The parameter can be filled from the input pipeline. | |
[Switch]$ValueFromPipeline, | |
# The parameter can be filled from a specific property in the input pipeline. | |
[Switch]$ValueFromPipelineByPropertyName, | |
# Assign the parameter to a specific parameter set. | |
[String]$ParameterSetName = "__AllParameterSets", | |
# Disallow null or empty values for the parameter if the parameter is specified. | |
[Switch]$ValidateNotNullOrEmpty, | |
# Test the parameter value using a regular expression. | |
[RegEx]$ValidatePattern, | |
# Regular expression options which dictate the behaviour of ValidatePattern. | |
[Text.RegularExpressions.RegexOptions]$ValidatePatternOptions = [Text.RegularExpressions.RegexOptions]::IgnoreCase, | |
# A minimum and maximum value to compare the argument to. | |
[Object[]]$ValidateRange, | |
# Test the parameter value using a script. | |
[ScriptBlock]$ValidateScript, | |
# Test the parameter value against a set of values. | |
[Object[]]$ValidateSet, | |
# ValidateSet can be configured to be case sensitive by setting this parameter to $false. The default behaviour for ValidateSet ignores case. | |
[Boolean]$ValidateSetIgnoreCase = $true | |
) | |
$AttributeCollection = @() | |
$ParameterAttribute = New-Object Management.Automation.ParameterAttribute | |
$ParameterAttribute.Mandatory = $Mandatory | |
$ParameterAttribute.Position = $Position | |
$ParameterAttribute.ValueFromPipeline = $ValueFromPipeline | |
$ParameterAttribute.ValueFromPipelineByPropertyName = $ValueFromPipelineByPropertyName | |
$AttributeCollection += $ParameterAttribute | |
if ($psboundparameters.ContainsKey('ValidateNotNullOrEmpty')) { | |
$AttributeCollection += New-Object Management.Automation.ValidateNotNullOrEmptyAttribute | |
} | |
if ($psboundparameters.ContainsKey('ValidatePattern')) { | |
$ValidatePatternAttribute = New-Object Management.Automation.ValidatePatternAttribute($ValidatePattern.ToString()) | |
$ValidatePatternAttribute.Options = $ValidatePatternOptions | |
$AttributeCollection += $ValidatePatternAttribute | |
} | |
if ($psboundparameters.ContainsKey('ValidateRange')) { | |
$AttributeCollection += New-Object Management.Automation.ValidateRangeAttribute($ValidateRange) | |
} | |
if ($psboundparameters.ContainsKey('ValidateScript')) { | |
$AttributeCollection += New-Object Management.Automation.ValidateScriptAttribute($ValidateScript) | |
} | |
if ($psboundparameters.ContainsKey('ValidateSet')) { | |
$ValidateSetAttribute = New-Object Management.Automation.ValidateSetAttribute($ValidateSet) | |
$ValidateSetAttribute.IgnoreCase = $ValidateSetIgnoreCase | |
$AttributeCollection += $ValidateSetAttribute | |
} | |
$Parameter = New-Object Management.Automation.RuntimeDefinedParameter($ParameterName, $ParameterType, $AttributeCollection) | |
if ($psboundparameters.ContainsKey('DefaultValue')) { | |
$Parameter.Value = $DefaultValue | |
} | |
return $Parameter | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment