Last active
January 25, 2024 01:40
-
-
Save joeypiccola/75cd1f4fd5c2e5f429d7dc580ce6b420 to your computer and use it in GitHub Desktop.
sample powershell function with multiple dynamic parameters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey Joey,
This code helped me a lot. I tried to solve the slow tab completion when enumerating information via Microsoft Graph, like getting the displayname for 3000+ users.
When the variables $TenantId, $ApplicationClientId and $CertificateThumbprint are set with the correct values, then the variable $FilterUserName will be available. I still search for a better solution. I saw that when the variable, like '$TabCompletionFunctionGetMgUsers=EnumerateWithPaging -Data ((Get-MgUser -All -Property DisplayName).DisplayName)' is set, that each time I use the tab completion it first executes the command (Get-MgUser -All...).
If hope you have a solution for this problem, like that DynamicParam only executes the variable once...this is for now the best that I could do...
[CmdLetBinding(DefaultParameterSetName="Default",
SupportsPaging
)]
Param (
[Parameter( ParameterSetName = "Default",Position = 0 , Mandatory )]$TenantId,
[Parameter( ParameterSetName = "Default",Position = 1 , Mandatory )]$ApplicationId,
[Parameter( ParameterSetName = "Default",Position = 2 , Mandatory )]$CertificateThumbprint
)
DynamicParam {
}
Begin {}
Process{}
End{}