private/Functions/New-DynamicParameter.ps1
Function New-DynamicParameter { [cmdletBinding( #SupportsShouldProcess = $true, #ConfirmImpact='high' )] Param( [Parameter( Mandatory=$true )] [String]$Name, [Parameter( Mandatory=$false )] [switch]$Mandatory=$false, [Parameter( Mandatory=$false )] [int]$Position, [Parameter( Mandatory=$false )] [string]$ParameterSetName, [Parameter( Mandatory=$true )] [String[]]$Enum, [Parameter( Mandatory=$false )] $DefaultValue ) $AttributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute] #Parameter Attributes $ParameterAttribute = New-Object System.Management.Automation.ParameterAttribute $ParameterAttribute.Mandatory = $Mandatory if($Position) { $ParameterAttribute.Position = $Position } if($ParameterSetName) { $ParameterAttribute.ParameterSetName = $ParameterSetName } $AttributeCollection.Add($ParameterAttribute) #Validation Logic if($Enum.length -gt 0) { $ValidateSetAttribute = New-Object System.Management.Automation.ValidateSetAttribute($Enum) $AttributeCollection.Add($ValidateSetAttribute) } $RuntimeParameter = New-Object System.Management.Automation.RuntimeDefinedParameter($Name, [string], $AttributeCollection) #Default Value if($DefaultValue) { $RuntimeParameter.Value = $DefaultValue } return $RuntimeParameter } |