Skip to content

Instantly share code, notes, and snippets.

@steviecoaster
Created August 16, 2018 16:05
Show Gist options
  • Save steviecoaster/2d6f8a19cb54295f6c8ed9f71e0b8907 to your computer and use it in GitHub Desktop.
Save steviecoaster/2d6f8a19cb54295f6c8ed9f71e0b8907 to your computer and use it in GitHub Desktop.
Function Test-VM {
[cmdletBinding()]
Param(
[Parameter]
[string]
$dummy
)
#Dynamic Params require [CmdletBinding()], but are defined on their own outside the main Param() block
DynamicParam {
$ParameterName = 'VM'
$RuntimeDict = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary
$Attribs = New-Object System.Collections.ObjectModel.Collection[System.Attribute]
$ParameterAttributes = New-Object System.Management.Automation.ParameterAttribute
$ParameterAttributes.Mandatory = $true
$ParameterAttributes.Position = 0
$Attribs.Add($ParameterAttributes)
$Dataset = Get-VM | Select-Object -ExpandProperty Name
$ValidateSet = New-Object System.Management.Automation.ValidateSetAttribute($Dataset)
$Attribs.Add($ValidateSet)
$RuntimeParam = New-Object System.Management.Automation.RuntimeDefinedParameter($ParameterName, [string[]], $Attribs)
$RuntimeDict.Add($ParameterName, $RuntimeParam)
return $RuntimeDict
}
#Pull out the Dynamic Param from BoundParameters so you can use it
Begin {$VM = $PSBoundParameters[$ParameterName]}
Process {
#Do stuff with that VM, here.
Write-Output "You selected: $VM"
}
End {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment