Skip to content

Instantly share code, notes, and snippets.

@brettmillerb
Created March 2, 2019 12:42
Show Gist options
  • Save brettmillerb/73943002436e60fe2047af5278ca7190 to your computer and use it in GitHub Desktop.
Save brettmillerb/73943002436e60fe2047af5278ca7190 to your computer and use it in GitHub Desktop.
Powershell Snippets
{
/*
// Place your snippets for PowerShell here. Each snippet is defined under a snippet name and has a prefix, body and
// description. The prefix is what is used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
// $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders. Placeholders with the
// same ids are connected.
// Example:
"Print to console": {
"prefix": "log",
"body": [
"console.log('$1');",
"$2"
],
"description": "Log output to console"
}
*/
"CalculatedProperty": {
"prefix": "Calculated-Property",
"body": [
"@{name='${1:PropertyName}';expression={${2:\\$_.PropertyValue}}}"
],
"description": "Creates a PSCustomObject"
},
"Parameter-Credential": {
"prefix": "Parameter-Credential",
"body": [
"[Parameter()]\r",
"[ValidateNotNull()]\r",
"[System.Management.Automation.PSCredential]\r",
"[System.Management.Automation.Credential()]\r",
"$$Credential${1: = [System.Management.Automation.PSCredential]::Empty}"
],
"description": "Parameter declaration snippet for a Credential parameter."
},
"Regex-Capture-Group": {
"prefix": "Regex-Capture-Group",
"body": [
"(?<${1:GroupName}>$0)",
],
"description": "Named Regex Capture Group."
},
"PesterDescribeContextIt": {
"prefix": "Describe-Context-It-Pester",
"body": [
"Describe \"${1:DescribeName}\" {",
"\tContext \"${2:ContextName}\" {",
"\t\tIt \"${3:ItName}\" {",
"\t\t\t${4:Assertion}",
"\t\t}$0",
"\t}",
"}"
],
"description": "Pester Describe block with nested Context & It blocks"
},
"PesterDescribeBlock": {
"prefix": "Describe-Pester",
"body": [
"Describe \"${1:DescribeName}\" {",
"\t$0",
"}"
],
"description": "Pester Describe block"
},
"PesterContextIt": {
"prefix": "Context-It-Pester",
"body": [
"Context \"${1:ContextName}\" {",
"\tIt \"${2:ItName}\" {",
"\t\t${3:Assertion}",
"\t}$0",
"}"
],
"description": "Pester - Context block with nested It block"
},
"PesterContext": {
"prefix": "Context-Pester",
"body": [
"Context \"${1:ContextName}\" {",
"\t$0",
"}"
],
"description": "Pester - Context block"
},
"PesterIt": {
"prefix": "It-Pester",
"body": [
"It \"${1:ItName}\" {",
"\t${2:Assertion}",
"}$0"
],
"description": "Pester - It block"
},
"IfShouldProcess": {
"prefix": "IfShouldProcess",
"body": [
"if (\\$PSCmdlet.ShouldProcess(\"${1:Target}\", \"${2:Operation}\")) {",
"\t$0",
"}"
],
"description": "Adds ShouldProcess block"
},
"Parameter-custom": {
"prefix": "parameter-custom",
"body": [
"[Parameter(${1:AttributeValues})]",
"[${2:ParameterType}]",
"$${0:ParameterName}"
],
"description": "Custom Parameter declaration snippet"
},
"Custom-Function": {
"prefix": "function-custom",
"body": [
"function ${1:Verb-Noun} {",
"\t[CmdletBinding(DefaultParameterSetName = '${2:Parameter Set 1}',",
"\t SupportsShouldProcess = \\$true,",
"\t ConfirmImpact = 'Medium')]",
"\t[OutputType([String])]",
"\tParam (",
"\t\t[Parameter(Mandatory=\\$true,",
"\t\t ValueFromPipeline = \\$true,",
"\t\t ValueFromPipelineByPropertyName = \\$true,",
"\t\t ParameterSetName = '${2:Parameter Set 1}')]",
"\t\t[ValidateNotNull()]",
"\t\t[ValidateNotNullOrEmpty()]",
"\t\t${3:\\$Param1},",
"",
"\t\t[Parameter(ParameterSetName = 'Another Parameter Set')]",
"\t\t[String]",
"\t\t${5:\\$Param2}",
"\t)",
"",
"\tbegin {",
"\t",
"\t}",
"",
"\tprocess {",
"\t\tif (\\$pscmdlet.ShouldProcess(\"Target\", \"Operation\")) {",
"\t\t\t$0",
"\t\t}",
"\t}",
"",
"\tend {",
"\t",
"\t}",
"}"
],
"description": "Example: a custom function removing unnecessary stuff from the extension snippet"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment