Skip to content

Instantly share code, notes, and snippets.

@jkavanagh58
Last active September 10, 2018 15:03
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/9312bd0d78eddc54d51998c0a5c9831a to your computer and use it in GitHub Desktop.
Save jkavanagh58/9312bd0d78eddc54d51998c0a5c9831a to your computer and use it in GitHub Desktop.
Customized PowerShell snippet file for Visual Studio Code
{
"PS Custom Object": {
"prefix": "customobj",
"body": [
"$$obj = [pscustomobject]@{",
"\t$FieldName = $FieldValue\r",
"}"
],
"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"
},
"Try catch": {
"prefix": "try",
"body": [
"try {",
"\t# Process to run\r",
"\t${1:code}\r",
"}",
"catch {\r",
"\t# Handle the failure\r",
"\t${2:error}",
"}"
],
"description": "Try catch statement"
},
"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"
},
"Try finallly": {
"prefix": "tryf",
"body": [
"try {",
"\t${_}",
"}",
"finally {",
"\t$0",
"}"
],
"description": "Try finally"
},
"While loop": {
"prefix": "while",
"body": [
"while (${$variable}) {",
"\t$0",
"}"
],
"description": "While loop"
},
"Class": {
"prefix": "class",
"body": [
"class ${Name} {",
"\t$0",
"}"
],
"description": "Class"
},
"Enum": {
"prefix": "enum",
"body": [
"enum ${Name} {",
"\t$0",
"}"
],
"description": "Enum"
},
"Method": {
"prefix": "meth",
"body": [
"[$1] $2() {",
"\t$0",
"}"
],
"description": "Method"
},
"Property": {
"prefix": "prop",
"body": [
"[${string}] ${$Property}$0"
],
"description": "Class property"
},
"Property hidden": {
"prefix": "proph",
"body": [
"hidden [${string}] ${$Property}$0"
],
"description": "Hidden class property"
},
"Function": {
"prefix": "func",
"body": [
"function $FunctionName {",
"\t$0# Code goes here\r",
"\\}"
],
"description": "Function"
},
"Function advanced": {
"prefix": "funcadv",
"body": [
"<#",
".SYNOPSIS\r",
"\tShort description\r",
".DESCRIPTION\r",
"\tLong description\r",
".EXAMPLE\r",
"\tExample of how to use this cmdlet\r",
".EXAMPLE\r",
"\tAnother example of how to use this cmdlet\r",
"#>\r",
"Function ${1:Verb}-${2:Noun} {\r",
"\t[CmdletBinding()]\r",
"\tparam(\r",
"\t\t[Parameter(Mandatory=$$true)]\r",
"\t\t[${ParameterType}]$${3:ParameterName}\r",
"\t)\r",
"\t\r",
"\tBEGIN {\r",
"\t\t${4:CodeHere}\r",
"\t}\r",
"\tPROCESS {",
"\t\t${5:CodeHere}\r",
"\t}\r",
"\tEND {",
"\t\t${6:CodeHere}\r",
"\t}\r"
],
"description": "Advanced function"
},
"My Comment Based Help": {
"prefix": "jjk - Help",
"body": [
"<#",
".SYNOPSIS",
"\tShort description",
".DESCRIPTION",
"\tLong description",
".PARAMETER Path",
"\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",
"\tSpecifies the object to be processed. You can also pipe the objects to this command.",
".EXAMPLE",
"\tC:\\PS>",
"\tExample of how to use this cmdlet",
".EXAMPLE",
"\tC:\\PS>",
"\tAnother example of how to use this cmdlet",
".INPUTS",
"\tInputs to this cmdlet (if any)",
".OUTPUTS",
"\tOutput from this cmdlet (if any)",
".NOTES",
"\t===========================================================================\r",
"\tCreated with:\tVisual Studio Code\r",
"\tCreated on:\t\t$datecreated\r",
"\tCreated by:\t\tKavanagh, John J.\r",
"\tOrganization:\tTEKSystems\r",
"\tFilename:\t\t$TM_FILENAME\r",
"\t===========================================================================\r",
"\t$date $initials: Enter first comment here\r",
"#>\r"
],
"description": "Help comment block - Customized for Wegmans"
},
"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": [
"\t[Parameter(Mandatory=\\$true,\r",
"\t\tValueFromPipeline=\\$true,\r",
"\t\tValueFromPipelineByPropertyName=\\$true,\r",
"\t\tHelpMessage = \"Enter Help Message Here\")]\r",
"\t[String]${2:parametername}\r"
],
"description": "Mandatory Parameter of String Type with Help Message"
},
"Mandatory Parameter with Validation Script": {
"prefix": "manparamvalidate",
"body": [
"\t[Parameter(Mandatory = $$true,\r",
"\t\tValueFromPipeline = $$true,\r",
"\t\tPosition = 1,\r",
"\t\tHelpMessage = \"Enter Help Message Here\")]\r",
"\t\t[ValidateScript({ ${2:ValidationScript} })]\r",
"\t[System.String]${3:paramName}\r"
],
"description": "Mandatory Parameter of a String with Validation Script"
},
"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} = @{\r",
"\t${2:Field} = ${3:fieldval}",
"}"
],
"description": "General Splatting Structure"
},
// Added last line as once you have entered variables to be removed you are done with this snippet
// 04.27.2018 JJ: Updated
"Initiate Garbage Collection": {
"prefix": "freememory",
"body":[
"Remove-Variable -Name ${1:variablestoberemoved}\r",
"[System.GC]::Collect()\r",
"$0"
],
"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"
},
// Can't break some habits
// Replace the alpha character "R" in my version for EST with the appropriate Time Zone
// https://www.timetemperature.com/abbreviations/military-time-zone-codes.shtml
"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"
},
// Adds verbose messaging in the BEGIN block
"Add Verbose in Begin block": {
"prefix": "verbBEGIN",
"body": ["Write-Verbose -Message \"[BEGIN]${1:Message}\""],
"description": "Add verbose messaging for activity in the BEGIN block"
},
// Adds verbose messaging in the PROCESS 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",
"\t\tHelpMessage = \"${3:HelpMessage}\")]\r",
"[${4:VariableType}]$${5:VariableName}"
],
"description": "Scaffold for Parameter creation"
},
"Parameter with Credential type": {
"prefix": "paramCredential",
"body": [
"[parameter(Mandatory=$${1|True,False|}, ValueFromPipeline=${2|$True,$False|},\r",
"\t\tHelpMessage = \"${3:HelpMessage}\")]\r",
"[System.Management.Automation.PSCredential]$${4:VariableName}"
],
"description": "Parameter statement for accepting a Credential Object"
},
// General Parameter Definition
"Parameter with ValidateSet Statement": {
"prefix": "paramvalidSet",
"body": [
"[parameter(Mandatory=$${1|True,False|}, ValueFromPipeline=${2|$True,$False|},\r",
"\t\tHelpMessage = \"${3:HelpMessage}\")]\r",
"\t[ValidateSet(${4:SetofValuestoVerify})]\r",
"[${5:VariableType}]$${6:VariableName}"
],
"description": "Parameter Framework with ValidateSet"
},
// Create Array for data collection
"Create Array List": {
"prefix": "newarray",
"body": ["$${1:ArrayName} = New-Object System.Collections.ArrayList\r"],
"description": "Creates a new array for data collect"
},
"Connect to Wegmans vSphere":{
"prefix": "wegviconnect",
"body": [
"If (!($$defaultViServer)){\r",
"\tConnect-VIserver $viServer -AllLinked | Out-Null\r",
"\t'You are now connected to {0} vSphere servers' -f $$defaultviservers.count\r",
"}\r"
],
"description": "Simple connect PowerCLI to the Wegmans vSphere environment"
},
"Connect to SRM Server and establish SRM API": {
"prefix": "srmAPIConnect",
"body": [
"$srmConnection = Connect-SrmServer -Credential $$admcreds -RemoteCredential $$admcreds\r",
"$srmApi = $srmConnection.ExtensionData\r"
],
"description": "With an active viServer connection, connect to SRM Host and establish API context"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment