Skip to content

Instantly share code, notes, and snippets.

@SadProcessor
Last active March 20, 2017 19:19
Show Gist options
  • Save SadProcessor/685518172d54b8b119bbd563a8d4d7a6 to your computer and use it in GitHub Desktop.
Save SadProcessor/685518172d54b8b119bbd563a8d4d7a6 to your computer and use it in GitHub Desktop.
Multiple Dynamic Param Template - Because Tab-Completion & Intellisense
## TEMPLATE MULTIPLE DYNAMIC PARAMETER ##
<#
.Synopsis
Test Dynamic Params
.DESCRIPTION
Multiple Dynamic Param Template
Because Tab-Completion & Intellisense
.EXAMPLE
Try me...
#>
function Test-DynamicParam(){
[CmdletBinding(DefaultParameterSetName='NoParam')]
[Alias('Dyn')]
Param(
#Selector in Fisrt position
[Parameter(Position=0,mandatory=$true,parameterSetName='SetOne')]
[Switch]$One,
[Parameter(Position=0,mandatory=$true,parameterSetName='SetTwo')]
[Switch]$Two,
[Parameter(Position=0,mandatory=$true,parameterSetName='SetThree')]
[Switch]$Three,
[Parameter(mandatory=$false)]
#Optional switch
[Switch]$Option
)
DynamicParam{
If($PSCmdlet.ParameterSetName -eq 'SetOne'){
## OneA - Mandatory in Pos1
# Create Attribute Object
$Attrib1A = New-Object System.Management.Automation.ParameterAttribute
$Attrib1A.Mandatory = $true
$Attrib1A.Position = 1
$Attrib1A.HelpMessage = "Choices for OneA: '1A1','1A2','1A3','1A4'"
# Create AttributeCollection object for the attribute Object
$Collection1A = new-object System.Collections.ObjectModel.Collection[System.Attribute]
# Add our custom attribute to collection
$Collection1A.Add($Attrib1A)
# Add Validate Set to attribute collection
$Values1A = '1A1','1A2','1A3','1A4'
$ValidateSet1A=new-object System.Management.Automation.ValidateSetAttribute($Values1A)
$Collection1A.Add($ValidateSet1A)
# Create Runtime Parameter with matching attribute collection
$DynParam1A = New-Object System.Management.Automation.RuntimeDefinedParameter('OneA', [String], $Collection1A)
## OneB
# Create Attribute Object
$Attrib1B = New-Object System.Management.Automation.ParameterAttribute
$Attrib1B.Mandatory = $false
$Attrib1B.HelpMessage = "Choices for OneB: '1B1','1B2','1B3','1B4'"
# Create AttributeCollection object for the attribute Object
$Collection1B = new-object System.Collections.ObjectModel.Collection[System.Attribute]
# Add our custom attribute
$Collection1B.Add($Attrib1B)
# Add Validate Set
$Values1B = '1B1','1B2','1B3','1B4'
$ValidateSet1B=new-object System.Management.Automation.ValidateSetAttribute($Values1B)
$Collection1B.Add($ValidateSet1B)
# Create Runtime Parameter with matching attribute collection
$DynParam1B = New-Object System.Management.Automation.RuntimeDefinedParameter('OneB', [String], $Collection1B)
# Create runtime Dictionary for this ParameterSet
$Dictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary
# Add all Runtime Params to dictionary
$Dictionary.Add('OneA', $dynParam1A)
$Dictionary.Add('OneB', $dynParam1B)
return $Dictionary
}
If($PSCmdlet.ParameterSetName -eq 'SetTwo'){
## TwoA
# Create Attribute Object
$Attrib2A = New-Object System.Management.Automation.ParameterAttribute
$Attrib2A.Mandatory = $false
$Attrib2A.HelpMessage = "Choices for TwoA: '2A1','2A2','2A3','2A4'"
# Create AttributeCollection object for the attribute Object
$Collection2A = new-object System.Collections.ObjectModel.Collection[System.Attribute]
# Add our custom attribute to collection
$Collection2A.Add($Attrib2A)
# Add Validate Set to attribute collection
$Values2A = '2A1','2A2','2A3','2A4'
$ValidateSet2A=new-object System.Management.Automation.ValidateSetAttribute($Values2A)
$Collection2A.Add($ValidateSet2A)
# Create Runtime Parameter with matching attribute collection
$DynParam2A = New-Object System.Management.Automation.RuntimeDefinedParameter('TwoA', [String], $Collection2A)
## TwoB
# Create Attribute Object
$Attrib2B = New-Object System.Management.Automation.ParameterAttribute
$Attrib2B.Mandatory = $true
$Attrib2B.HelpMessage = "Choices for TwoB: '2B1','2B2','2B3','2B4'"
# Create AttributeCollection object for the attribute Object
$Collection2B = new-object System.Collections.ObjectModel.Collection[System.Attribute]
# Add our custom attribute
$Collection2B.Add($Attrib2B)
# Add Validate Set
$Values2B = '2B1','2B2','2B3','2B4'
$ValidateSet2B=new-object System.Management.Automation.ValidateSetAttribute($Values2B)
$Collection2B.Add($ValidateSet2B)
# Create Runtime Parameter with matching attribute collection
$DynParam2B = New-Object System.Management.Automation.RuntimeDefinedParameter('TwoB', [String], $Collection2B)
## Create runtime Dictionary for this ParameterSet
$Dictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary
# Add all Runtime Params to dictionary
$Dictionary.Add('TwoA', $dynParam2A)
$Dictionary.Add('TwoB', $dynParam2B)
return $Dictionary
}
If($PSCmdlet.ParameterSetName -eq 'SetThree'){
## ThreeA
# Create Attribute Object
$Attrib3A = New-Object System.Management.Automation.ParameterAttribute
$Attrib3A.Mandatory = $false
$Attrib3A.HelpMessage = "Choices for ThreeA: '3A1','3A2','3A3','3A4'"
# Create AttributeCollection object for the attribute Object
$Collection3A = new-object System.Collections.ObjectModel.Collection[System.Attribute]
# Add our custom attribute to collection
$Collection3A.Add($Attrib3A)
# Add Validate Set to attribute collection
$Values3A = '3A1','3A2','3A3','3A4'
$ValidateSet3A=new-object System.Management.Automation.ValidateSetAttribute($Values3A)
$Collection3A.Add($ValidateSet3A)
# Create Runtime Parameter with matching attribute collection
$DynParam3A = New-Object System.Management.Automation.RuntimeDefinedParameter('ThreeA', [String], $Collection3A)
## ThreedB
# Create Attribute Object
$Attrib3B = New-Object System.Management.Automation.ParameterAttribute
$Attrib3B.Mandatory = $false
$Attrib3B.HelpMessage = "Choices for ThreeA: '3B1','3B2','3B3','3B4'"
# Create AttributeCollection object for the attribute Object
$Collection3B = new-object System.Collections.ObjectModel.Collection[System.Attribute]
# Add our custom attribute
$Collection3B.Add($Attrib3B)
# Add Validate Set
$Values3B = '3B1','3B2','3B3','3B4'
$ValidateSet3B=new-object System.Management.Automation.ValidateSetAttribute($Values3B)
$Collection3B.Add($ValidateSet3B)
# Create Runtime Parameter with matching attribute collection
$DynParam3B = New-Object System.Management.Automation.RuntimeDefinedParameter('ThreeB', [String], $Collection3B)
## Create runtime Dictionary for this ParameterSet
$Dictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary
# Add all Runtime Params to dictionary
$Dictionary.Add('ThreeA', $dynParam3A)
$Dictionary.Add('ThreeB', $dynParam3B)
return $Dictionary
}
}
begin{
#BeginBlock
If($PSCmdlet.ParameterSetName -eq 'NoParam'){Help Test-DynamicParam; Break}
Clear
}
Process{
#ProcessBlock
If($PSCmdlet.ParameterSetName -eq 'SetOne'){$string = 'Hello'}
If($PSCmdlet.ParameterSetName -eq 'SetTwo'){$string = 'World'}
If($PSCmdlet.ParameterSetName -eq 'SetThree'){$string = 'HelloWorld'}
}
end{
#EndBlock
return $String
}
}
#####EndFunction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment