Skip to content

Instantly share code, notes, and snippets.

@Seekatar
Created November 20, 2018 15:43
Show Gist options
  • Save Seekatar/a8638191e341f39d1555fa5ff19a0ee5 to your computer and use it in GitHub Desktop.
Save Seekatar/a8638191e341f39d1555fa5ff19a0ee5 to your computer and use it in GitHub Desktop.
Helper script for making dynamic parameters in PowerShell
<#
.SYNOPSIS
Helper for making dynamic parameters with ValidateSets in PowerShell
.PARAMETER ParameterName
Name of dynamic parameter
.PARAMETER MakeList
A scriptblock to create the ValidateSet list
.PARAMETER Alias
Alias for parameter
.PARAMETER ParameterSetName
Parameter set name
.PARAMETER Mandatory
If the parameter is mandatory
.PARAMETER ValueFromPipeline
If the parameter is from the pipeline
.PARAMETER Position
Position to set
.PARAMETER Help
Optional help
.PARAMETER DebugFile
File for outputing debug information, for debugging dyn parameters
.EXAMPLE
function simpleDynamicParam {
[CmdletBinding()]
param()
DynamicParam
{
makeDynamicParam "dyn" -MakeList {
return 'cow','pig','horse'
}
}
process
{
Set-StrictMode -Version Latest
$dyn = $psboundparameters["dyn"]
Write-Verbose "params are $($psboundparameters | out-string)"
$dyn
}
}
.EXAMPLE
function conditionalDynamicParam {
[CmdletBinding()]
param(
[switch] $Colors
)
DynamicParam
{
makeDynamicParam "dyn" -MakeList {
if ( !((Test-Path variable:colors) -and $colors )
{
'red','light blue','green'
}
}
}
#>
function makeDynamicParam
{
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[string] $ParameterName,
[Parameter(Mandatory)]
[scriptblock] $MakeList,
[string] $Alias,
[string] $ParameterSetName,
[switch] $Mandatory,
[switch] $ValueFromPipeline,
[int] $Position = 0,
[string] $Help,
[string] $DebugFile
)
Set-StrictMode -Version Latest
function logit($msg) {
if ( $DebugFile ) { "$(Get-Date -form 's') $msg" | out-file $DebugFile -Append -Encoding utf8 }
}
logit "makeDynamicParam for $ParameterName"
# create a dictionary to return
$paramDictionary = new-object -Type System.Management.Automation.RuntimeDefinedParameterDictionary
# create a new [string] dyn parameter with a collection of attributes
$attributeCollection = new-object -Type System.Collections.ObjectModel.Collection[System.Attribute]
$dynParam = new-object -Type System.Management.Automation.RuntimeDefinedParameter($ParameterName, [String], $attributeCollection)
# create a new atrbute for all parameter sets
$attributes = new-object System.Management.Automation.ParameterAttribute
if ( $ParameterSetName )
{
$attributes.ParameterSetName = $ParameterSetName
}
if ( $Help )
{
$attributes.HelpMessage = $Help
}
$attributes.Mandatory = [bool]$Mandatory
$attributes.ValueFromPipeline = [bool]$ValueFromPipeline
$attributes.Position = $Position
logit "Attributes are $(ConvertTo-Json ($attributes | Select-Object * -ExcludeProperty "TypeId") -Depth 1)"
try
{
logit "About to invoke"
$names = $MakeList.Invoke()
logit "list is now $($names | out-string)"
if ( $names )
{
$paramOptions = New-Object System.Management.Automation.ValidateSetAttribute -ArgumentList $names
$attributeCollection.Add($paramOptions)
}
}
catch {
logit "Exception is $_"
}
# hook things together
$attributeCollection.Add($attributes)
$paramDictionary.Add($ParameterName, $dynParam)
return $paramDictionary
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment