Last active
April 29, 2023 05:36
-
-
Save quonic/a36b8d1529518aaa77577d0dfaa58f43 to your computer and use it in GitHub Desktop.
A Powershell DynamicParam snippet for VSCode
This file contains hidden or 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
{ | |
"Dynamic_Param_Start": { | |
"prefix": "dynamic1start", | |
"body": [ | |
"DynamicParam {", | |
"\t${1:dynamic2p}${2:}", | |
"}" | |
], | |
"description": "An empty dynamic parameter" | |
}, | |
"Dynamic_Param_Body": { | |
"prefix": "dynamic2param", | |
"body": [ | |
"### Parameter Name Start: ${1:ParamName}", | |
"", | |
"# Create the attributes for a dynamic parameter", | |
"\\$AttributeCollection = @(", | |
"\t[System.Management.Automation.ParameterAttribute]@{", | |
"\t\tHelpMessage = 'Help Message Here!'", | |
"\t\tMandatory = \\$true", | |
"\t\tValueFromPipelineByPropertyName = \\$true", | |
"\t}", | |
")", | |
"", | |
"# Create a dynamic parameter", | |
"\\$${1:ParamName} = [System.Management.Automation.RuntimeDefinedParameter]::new('${1:ParamName}', [${2:string}], \\$AttributeCollection)", | |
"", | |
"# Create the dictionary", | |
"\\$ParamDictionary = [System.Management.Automation.RuntimeDefinedParameterDictionary]::new()", | |
"", | |
"# Add the dynamic parameter(s)", | |
"\\$ParamDictionary.Add(\\$${1:ParamName}.Name, \\$${1:ParamName})", | |
"", | |
"# Parameter Name End: ${1:ParamName}", | |
"" | |
], | |
"description": "A simple dynamic parameter" | |
}, | |
"Dynamic_Param_Body_Array": { | |
"prefix": "dynamic2paramarray", | |
"body": [ | |
"### Parameter Name Start: ${1:ParamName}", | |
"", | |
"# Generate a list of ValidateSet items", | |
"\\$ValidateSetAttributeArray = @(${3:array})", | |
"", | |
"# Create the attributes for a dynamic parameter", | |
"\\$AttributeCollection = @(", | |
"\t[System.Management.Automation.ParameterAttribute]@{", | |
"\t\tHelpMessage = 'Help Message Here!'", | |
"\t\tMandatory = \\$true", | |
"\t\tValueFromPipelineByPropertyName = \\$true", | |
"\t}", | |
"\t", | |
"[System.Management.Automation.ValidateSetAttribute][${4:string}[]]\\$ValidateSetAttributeArray", | |
")", | |
"", | |
"# Create a dynamic parameter", | |
"\\$${1:ParamName} = [System.Management.Automation.RuntimeDefinedParameter]::new('${1:ParamName}', [${2:string}], \\$AttributeCollection)", | |
"", | |
"# Create the dictionary", | |
"\\$ParamDictionary = [System.Management.Automation.RuntimeDefinedParameterDictionary]::new()", | |
"", | |
"# Add the dynamic parameter(s)", | |
"\\$ParamDictionary.Add(\\$${1:ParamName}.Name, \\$${1:ParamName})", | |
"", | |
"# Parameter Name End: ${1:ParamName}", | |
"" | |
], | |
"description": "A simple dynamic parameter with a Validate Set" | |
}, | |
"Dynamic_Param_End": { | |
"prefix": "dynamic3end", | |
"body": [ | |
"# return the ParameterDictionary", | |
"return \\$ParamDictionary" | |
], | |
"description": "The last part of the dynamic parameter" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Rewrote it with three parts, the first bracket. Typing "dynamic1start" will get the process started. Hitting tab then typing "a" will bring up the two options for either a Parameter with out a Set or with a Set. Hitting enter on either of those will create a new Parameter that you can change the name and type(string for example). After the last tab you can type "dynamic2" to start creating another Parameter, or type "dynamic3" and it will have the last part to complete the Dynamic Parameter.