sample powershell function with multiple dynamic parameters
Function New-VMDeploy { | |
[CmdletBinding()] | |
Param() | |
DynamicParam { | |
# Set the dynamic parameters' name | |
$ParamName_portgroup = 'PortGroup' | |
# Create the collection of attributes | |
$AttributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute] | |
# Create and set the parameters' attributes | |
$ParameterAttribute = New-Object System.Management.Automation.ParameterAttribute | |
$ParameterAttribute.Mandatory = $true | |
$ParameterAttribute.Position = 1 | |
# Add the attributes to the attributes collection | |
$AttributeCollection.Add($ParameterAttribute) | |
# Create the dictionary | |
$RuntimeParameterDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary | |
# Generate and set the ValidateSet | |
$arrSet = (Get-VDPortgroup | ?{$_.name -like "*VLAN*"}).name | |
$ValidateSetAttribute = New-Object System.Management.Automation.ValidateSetAttribute($arrSet) | |
# Add the ValidateSet to the attributes collection | |
$AttributeCollection.Add($ValidateSetAttribute) | |
# Create and return the dynamic parameter | |
$RuntimeParameter = New-Object System.Management.Automation.RuntimeDefinedParameter($ParamName_portgroup, [string], $AttributeCollection) | |
$RuntimeParameterDictionary.Add($ParamName_portgroup, $RuntimeParameter) | |
# Set the dynamic parameters' name | |
$ParamName_datastore = 'Datastore' | |
# Create the collection of attributes | |
$AttributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute] | |
# Create and set the parameters' attributes | |
$ParameterAttribute = New-Object System.Management.Automation.ParameterAttribute | |
$ParameterAttribute.Mandatory = $true | |
$ParameterAttribute.Position = 2 | |
# Add the attributes to the attributes collection | |
$AttributeCollection.Add($ParameterAttribute) | |
# Generate and set the ValidateSet | |
$arrSet = (Get-Datastore).name | |
$ValidateSetAttribute = New-Object System.Management.Automation.ValidateSetAttribute($arrSet) | |
# Add the ValidateSet to the attributes collection | |
$AttributeCollection.Add($ValidateSetAttribute) | |
# Create and return the dynamic parameter | |
$RuntimeParameter = New-Object System.Management.Automation.RuntimeDefinedParameter($ParamName_datastore, [string], $AttributeCollection) | |
$RuntimeParameterDictionary.Add($ParamName_datastore, $RuntimeParameter) | |
return $RuntimeParameterDictionary | |
} | |
begin{} | |
process { | |
$portgroup = $PsBoundParameters[$ParamName_portgroup] | |
$datastore = $PsBoundParameters[$ParamName_datastore] | |
} | |
end {} | |
} |
This comment has been minimized.
This comment has been minimized.
This was the golden ticket for my custom VM deployment function! I no longer have to maintain a static list inside the parameter's validateset for hundreds of VM templates, networks, portgroups, etc. Setting the ParameterAttribute.Postition = 0 on all the dynamic params allows intelisense to work (not setting will break intelisense). There is also considerable lag when pulling info from vCenter (we have a big environment) so I made a scheduled task to keep the info in simple txt files that the function can quickly read from. I may switch that over to a SQL DB later. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This comment has been minimized.
This worked great, I tried
Register-ArgumentCompleter, ArgumentCompleter
but those seems working only on ISE.