Skip to content

Instantly share code, notes, and snippets.

@jkavanagh58
Created November 12, 2019 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jkavanagh58/ee0f0696ce6e4379d9e5dedeaf1ce3f9 to your computer and use it in GitHub Desktop.
Save jkavanagh58/ee0f0696ce6e4379d9e5dedeaf1ce3f9 to your computer and use it in GitHub Desktop.
Current VSCode PowerShell Snippet file
{
"Day of Week Validated Parameter": {
"prefix": "DOWParam",
"body": [
"[Parameter(Mandatory = $$true,\r",
"\tValueFromPipeline = $$true,\r",
"\tHelpMessage = \"${1:Enter Help Message Here}\")]\r",
"[ValidateSet('Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday')]\r",
"[System.String]$${2:parametername}\r"
],
"description": "Creates a Day of Week param with validation is entered."
},
"Credential Parameter": {
"prefix": "credparam",
"body": [
"\t[Parameter(Mandatory = $$false,\r",
"\t\tValueFromPipeline = $$true,\r",
"\t\tHelpMessage = 'Ensure you are using the correct credentials for this operation')]\r",
"\t[System.Management.Automation.PSCredential]$$Credential\r"
],
"description": "Add a credential object parameter"
},
"PS Custom Object": {
"prefix": "custompsobj",
"body": [
"$$obj = [pscustomobject]@{",
"\tFieldName = FieldValue;",
"}"
],
"description": "Skeleton for adding a PowerShell Custom Object"
},
"Do...while loop": {
"prefix": "do",
"body": [
"do {",
"\t$0",
"} while (${$variable});"
],
"description": "Do...while loop"
},
"Do...until loop": {
"prefix": "do",
"body": [
"do {",
"\t$0",
"} until (${$variable});"
],
"description": "Do...until loop"
},
"Else statement": {
"prefix": "else",
"body": [
"else {",
"\t$0",
"}"
],
"description": "Else statement"
},
"ElseIf statement": {
"prefix": "elseif",
"body": [
"elseif (${$variable}) {",
"\t$0",
"}"
],
"description": "Elseif statement"
},
"Foreach statement": {
"prefix": "foreach",
"body": [
"foreach (${$item} in ${$collection}) {",
"\t$0",
"}"
],
"description": "Foreach statement"
},
"For loop": {
"prefix": "for",
"body": [
"for (${$i} = 0; ${$i} -lt ${$length}; ${$i}++) {",
"\t$0",
"}"
],
"description": "For loop"
},
"If statement": {
"prefix": "if",
"body": [
"if (${$variable}) {",
"\t$0",
"}"
],
"description": "If statement"
},
"Reverse for loop": {
"prefix": "forr",
"body": [
"for (${$i} = ${$length} - 1; ${$i} -ge 0; ${$i}--) {",
"\t$0",
"}"
],
"description": "Reverse for loop"
},
"Switch statement": {
"prefix": "switch",
"body": [
"switch (${$variable}) {",
"\t${condition} { ${action}; break }",
"\tDefault {}",
"}"
],
"description": "Switch statement"
},
"My Try catch": {
"prefix": "mytry",
"body": [
"try {",
"\t${Process}",
"}",
"catch [${System.Exception}] {",
"\t\"Message: {0}\" -f $$_.Exception.Message\r",
"}"
],
"description": "Try catch"
},
"Try catch finally": {
"prefix": "trycf",
"body": [
"try {",
"\t# Process to run\r",
"\t${1:code}\r",
"}",
"catch {\r",
"\t# Handle the failure\r",
"\t${2:error}",
"}\r",
"finally {\r",
"\t# Finalize try statement\r",
"\t${3:finally}",
"}"
],
"description": "Try catch finally statement"
},
"Class": {
"prefix": "class",
"body": [
"class ${Name} {",
"\t$0",
"}"
],
"description": "Class"
},
"Function": {
"prefix": "func",
"body": [
"function $1() {",
"\t$0",
"}"
],
"description": "Function"
},
"Function advanced": {
"prefix": "funcadv",
"body": [
"<#",
".SYNOPSIS",
"\tShort description",
".DESCRIPTION",
"\tLong description",
".EXAMPLE",
"\tPS>_ \r",
"\tExample of how to use this function\r",
"#>",
"function ${verb}-${noun} {",
"\t[CmdletBinding()]",
"\t[OutputType([${int}])]",
"\tParam(",
"\t\t[Parameter(Mandatory=$true)]",
"\t\t[${string}]",
"\t\t${Param1}",
"\t)",
"\t",
"\tBEGIN {",
"\t}",
"\t",
"\tPROCESS {",
"\t\t$0",
"\t}",
"\t",
"\tEND {",
"\t}",
"}"
],
"description": "Advanced function"
},
"Help": {
"prefix": "PSDocHelp",
"body": [
"<#\r",
".SYNOPSIS\r",
"\tShort description",
".DESCRIPTION\r",
"\tLong description",
".PARAMETER Path\r",
"\tSpecifies a path to one or more locations.",
".PARAMETER LiteralPath",
"\tSpecifies a path to one or more locations. Unlike Path, the value of LiteralPath is used exactly as it",
"\tis typed. No characters are interpreted as wildcards. If the path includes escape characters, enclose",
"\tit in single quotation marks. Single quotation marks tell Windows PowerShell not to interpret any",
"\tcharacters as escape sequences.",
".PARAMETER InputObject\r",
"\tSpecifies the object to be processed. You can also pipe the objects to this command.",
".EXAMPLE\r",
"\tPS>_ \r",
"\tExample of how to use this cmdlet",
".EXAMPLE\r",
"\tPS>_ \r",
"\tAnother example of how to use this script.\r",
".INPUTS\r",
"\tInputs to this cmdlet (if any)\r",
".OUTPUTS\r",
"\tOutput from this cmdlet (if any)\r",
".NOTES\r",
"\t===========================================================================\r",
"\tCreated with:\tVisual Studio Code\r",
"\tCreated on:\t\t$CURRENT_MONTH.$CURRENT_DATE.$CURRENT_YEAR\r",
"\tCreated by:\t\tJohn J. Kavanagh\r",
"\tOrganization:\t$organization\r",
"\tFilename:\t\t$TM_FILENAME\r",
"\t===========================================================================\r",
"\t$date $initials: Enter first comment here\r",
"#>\r"
],
"description": "Help comment block - Customized"
},
"Function CBH": {
"prefix": "PSFuncHelp",
"body": [
"<#\r",
".SYNOPSIS\r",
"\tShort description",
".DESCRIPTION\r",
"\tLong description",
".PARAMETER Path\r",
"\tSpecifies a path to one or more locations.",
".PARAMETER LiteralPath",
"\tSpecifies a path to one or more locations. Unlike Path, the value of LiteralPath is used exactly as it",
"\tis typed. No characters are interpreted as wildcards. If the path includes escape characters, enclose",
"\tit in single quotation marks. Single quotation marks tell Windows PowerShell not to interpret any",
"\tcharacters as escape sequences.",
".PARAMETER InputObject\r",
"\tSpecifies the object to be processed. You can also pipe the objects to this command.",
".EXAMPLE\r",
"\tPS>_ \r",
"\tExample of how to use this cmdlet",
".EXAMPLE\r",
"\tPS>_ \r",
"\tAnother example of how to use this script.\r",
".INPUTS\r",
"\tInputs to this cmdlet (if any)\r",
".OUTPUTS\r",
"\tOutput from this cmdlet (if any)\r",
".NOTES\r",
"\t===========================================================================\r",
"\tCreated with:\tVisual Studio Code\r",
"\tCreated on:\t\t$CURRENT_MONTH.$CURRENT_DATE.$CURRENT_YEAR\r",
"\tCreated by:\t\t${1:Developer}\r",
"\tOrganization:\t${2:Organization}\r",
"\tFunction Name:\t${3:FunctionName}\r",
"\t===========================================================================\r",
"\t$date $initials: Enter first comment here\r",
"#>\r"
],
"description": "Help comment block - Functions"
},
"CmdletBinding - Supports Whatif": {
"prefix": "cmdletbinding",
"body": [
"[CmdletBinding(SupportsShouldProcess=$$true,ConfirmImpact='Medium')]\r",
"Param(\r",
"\t\r",
")\r"
],
"description": "Cmdletbinding with support for whatif"
},
"Mandatory Parameter with Help Message": {
"prefix": "manparamhelp",
"body": [
"[Parameter(Mandatory=$$true,\r",
"\tValueFromPipeline=$$true,\r",
"\tValueFromPipelineByPropertyName=$$true,\r",
"\tHelpMessage = \"Enter Help Message Here\")]\r",
"\t[String]${2:parametername}\r"
],
"description": "Manadatory Parameter of String Type with Help Message"
},
"Mandatory Parameter with Validation Script": {
"prefix": "manparamscriptvalidate",
"body": [
"[parameter(Mandatory=$${1|True,False|}, ValueFromPipeline=${2|$True,$False|})\r",
"\tHelpMessage = \"${3:HelpMessage}\")]\r",
"[ValidateScript({ ${4:ValidationScript} })]\r",
"[${5:VariableType}]$${6:paramName}\r"
],
"description": "Mandatory Parameter of a String with Validation Script"
},
"Mandatory Parameter with Validation Set": {
"prefix": "manparamvalidateset",
"body": [
"[Parameter(Mandatory = $$true,\r",
"\tValueFromPipeline = $$true,\r",
"\tHelpMessage = \"Enter Help Message Here\")]\r",
"[ValidateSet(${2:ValidDataSet})]\r",
"[System.String]${3:paramName}\r"
],
"description": "Mandatory Parameter of a String with set of values to validate against."
},
"Standard Logging": {
"prefix": "addlog",
"body": [
"Function write-logevent {\r",
"Param(\r",
"\t[String]$$script:Logfile = 'c:\\etc\\testlog.txt',\r",
"\t[String]$$logText,\r",
"\t[String]$$logTime = (get-date -UFormat %m%d%Y%H%M%S)\r",
")\r",
"'{0}:{1}' -f $$logTime, $$logText | out-file $$script:logfile -Append\r",
"} #End write-logevent\r"
],
"description": "Need to develop a standard logging function/method"
},
"Send Mail Message": {
"prefix": "mailsnippet",
"body": [
"$$mailparams = @{\r",
"\tTo = \"$toaddress\"\r",
"\tFrom = \"$fromaddress\"\r",
"\tSubject = \"$mailsubject\"\r",
"\tBody = \"$mailmessage\"\r",
"\tbodyAsHTML = $$True\r",
"\tSMTPServer = \"smtp.somedoamin.com\"\r",
"\tErrorAction = \"Stop\"\r",
"}\r",
"Try {\r",
"\tsend-mailmessage @mailparams\r",
"}\r",
"Catch {\r",
"\t\"Unable to send message\"\r",
"\t\"Reason: {0}:\" -f $$error[0].exception.message\r",
"}"
],
"description": "Splatted send mail message"
},
"General Splat": {
"prefix": "splatsnippet",
"body": [
"${1:\\$varname} = [pscustomobject]@{\r",
"\t$Field = $fieldval",
"}"
],
"description": "General Splatting Structure"
},
"Initiate Garbage Collection": {
"prefix": "freememory",
"body": [
"Remove-Variable -Name ${1:variablestoberemoved}\r",
"[System.GC]::Collect()\r"
],
"description": "Free up memory when script completes"
},
"Count Files in Folder and Subfolders": {
"prefix": "filecount",
"body": [
"${1:\\$paramName} = (Get-ChildItem -Path ${2:ProvideAPath} | Measure-Object).Count\r"
],
"description": "Performs a count on all files in a folder, including subfolders"
},
"Array List using Collection": {
"prefix": "collarray",
"body": [
"# Create New Array\r",
"$${1:arrayName} = [System.Collections.Generic.List[String]]::new()\r",
"# Add items to array\r",
"$${1}.Add(\"${2:ItemValue}\") | Out-Null"
],
"description": "Form Array using .Net Collection"
},
"LogFile Compact Mil DTG": {
"prefix": "mildtg",
"body": [
"Get-Date -UFormat '%d%H%MR%b%y'"
],
"description": "Uses Get-Date with UFormat to specify Military DTG"
},
"Add Verbose in Begin block": {
"prefix": "verbBEGIN",
"body": [
"Write-Verbose -Message \"[BEGIN]${1:Message}\""
],
"description": "Add verbose messaging for activity in the BEGIN block"
},
"Add Verbose in Process block": {
"prefix": "verbPROCESS",
"body": [
"Write-Verbose -Message \"[PROCESS]${1:Message}\""
],
"description": "Add verbose messaging for activity in the PROCESS block"
},
// Adds verbose messaging in the END block
"Add Verbose in END": {
"prefix": "verbEND",
"body": [
"Write-Verbose -Message \"[END]${1:Message}\""
],
"description": "Add verbose messaging for activity in the END section"
},
// Adds error messaging
"Add Error Message": {
"prefix": "errMessage",
"body": [
"Write-Error -Message \"${1:Message}\""
],
"description": "Add verbose messaging for activity in the BEGIN section"
},
// General Parameter Definition
"Parameter Statement": {
"prefix": "paramStatement",
"body": [
"[parameter(Mandatory = $${1|True,False|}, ValueFromPipeline = ${2|$True,$False|},\r",
"\tHelpMessage = \"${3:HelpMessage}\")]\r",
"[${4:VariableType}]$${5:VariableName}"
],
"description": "Scaffold for Parameter creation"
},
"swith Parameter Statement": {
"prefix": "paramSwitchStatement",
"body": [
"[parameter(Mandatory=$${1|True,False|}, ValueFromPipeline=${2|$True,$False|})\r",
"\t\tHelpMessage = \"${3:HelpMessage}\")]\r",
"[Switch]$${4:VariableName}"
],
"description": "Scaffold for Parameter creation"
},
"Begin Process End Block": {
"prefix": "BeginProcessEnd",
"body": [
"BEGIN {\r",
"\t# Start Begin Block\r",
"}\r",
"PROCESS {\r",
"\t# Start Process Block\r",
"}\r",
"END {\r",
"\t# Start End Block\r",
"}\r"
]
},
"Pester - Minimal": {
"prefix": "pestermin",
"body": [
"Describe '${Description}' {\r",
"\t\r",
"}"
],
"description": "Basic Pester Test"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment