Functions/Scripting/Parametrics/New-StaticParameter.ps1
<#
.Synopsis This script creates a static runtime parameter. .DESCRIPTION This script creates a static runtime parameter that can attached to a RuntimeParameterDictionary inside of a dynamicparam block for better cmdlet construction. It takes in Parameter Name, Position, Dataset (to enumerate in the dynamic parameter) and mandatory status. .EXAMPLE New-StaticParameter -ParamName VMHost .EXAMPLE New-StaticParameter -ParamName VMHost -Mandatory $True -Position 0 #> Function New-StaticParameter { [CmdletBinding()] Param ( [Parameter(Mandatory=$true)] [string] $ParamName, [Parameter(Mandatory=$true)] [ValidateSet([string],[string[]],[int],[int[]],[boolean],[boolean[]],[psobject],[psobject[]],[switch],[pscredential],[pscredential[]],[hashtable],[hashtable[]])] $ValueType, [Parameter(Mandatory=$false)] [Boolean] $Mandatory = $true, [Parameter(Mandatory=$false)] $DefaultValue, [Parameter(Mandatory=$false)] $ValueFromPipeLine = $false, [Parameter(Mandatory=$false)] [string[]] $ParameterSetName, [Parameter(Mandatory=$false)] [int] $Position ) Process { # Build new Attribute Collection $AttributeCollection = [System.Collections.ObjectModel.Collection[System.Attribute]]::new() if ($ParameterSetName -and $ParameterSetName.count -gt 1) { Foreach ($PSN in $ParameterSetName) { $ParameterAttribute = [System.Management.Automation.ParameterAttribute]::new() $ParameterAttribute.Mandatory = $Mandatory $ParameterAttribute.ValueFromPipeline = $ValueFromPipeLine if($position){$ParameterAttribute.Position = $Position} $ParameterAttribute.ParameterSetName = $PSN $AttributeCollection.Add($ParameterAttribute) } } else { $ParameterAttribute = [System.Management.Automation.ParameterAttribute]::new() $ParameterAttribute.Mandatory = $Mandatory $ParameterAttribute.ValueFromPipeline = $ValueFromPipeLine if($position){$ParameterAttribute.Position = $Position} if($ParameterSetName){$ParameterAttribute.ParameterSetName = [array]($ParameterSetName)} $AttributeCollection.Add($ParameterAttribute) } # Push Attribute Collection into the Runtime Parameter $RuntimeParameter = [System.Management.Automation.RuntimeDefinedParameter]::new($ParamName,$ValueType,$AttributeCollection) if (!$Mandatory -and $DefaultValue){$RuntimeParameter.Value = $DefaultValue} Return $RuntimeParameter } } |