Skip to content

Instantly share code, notes, and snippets.

@jkav58
Created August 3, 2023 15:51
Show Gist options
  • Save jkav58/183578a5e2f40234a6e018ff58009023 to your computer and use it in GitHub Desktop.
Save jkav58/183578a5e2f40234a6e018ff58009023 to your computer and use it in GitHub Desktop.
VSCode Custom PowerShell Snippets
{
"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": "jjk - 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_YEAR-$CURRENT_MONTH-$CURRENT_DATE $CURRENT_HOUR:$CURRENT_MINUTE:$CURRENT_SECOND\r",
"\tCreated by:\t\tAuthor/Developer Name\r",
"\tOrganization:\tMednax\r",
"\tFilename:\t\t$TM_FILENAME\r",
"\t===========================================================================\r",
"\t$date $initials: Enter first comment here\r",
"#>\r"
],
"description": "Help comment block - Customized"
},
"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": "manparamvalidate",
"body": [
"\t[Parameter(Mandatory = $$true,\r",
"\tValueFromPipeline = $$true,\r",
"\tPosition = 1,\r",
"\tHelpMessage = \"Enter Help Message Here\")]\r",
"\t[ValidateScript({ ${2:ValidationScript} })]\r",
"\t[System.String]${3: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": "jkav_freememory",
"body": [
"\tRemove-Variable -Name ${1:variablestoberemoved}\r",
"\t[System.GC]::Collect()"
],
"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"
},
"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"
},
"Add Verbose in END": {
"prefix": "verbEND",
"body": [
"Write-Verbose -Message \"[END]${1:Message}\""
],
"description": "Add verbose messaging for activity in the END section"
},
"Add Error Message": {
"prefix": "errMessage",
"body": [
"Write-Error -Message \"${1:Message}\""
],
"description": "Add verbose messaging for activity in the BEGIN section"
},
"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"
},
"switch 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 Switch 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"
},
"Array - Declaration": {
"prefix": "psarray",
"body": "$${1:VariableName} = [System.Collections.ArrayList]@()",
"description": "Formal array declaration"
},
"Get PDC": {
"prefix": "jjk - getadPDC",
"body": [
"# Determine DomainController to direct AD actions\r",
"$$targetDC = (Get-ADDomain).PDCEmulator\r"
],
"description": "Programatic method to determine active PDC"
},
"AD User Query": {
"prefix": "jjk - AD User Query",
"body": [
"$$adQry = @{\r",
"\tSearchBase = 'OU=Associates,DC=corp,DC=sagentlending,DC=com'\r",
"\tFilter = { Enabled -eq 'True' }\r",
"\tProperties = 'Company'\r",
"\tServer = $$targetDC\r",
"}\r",
"$$users = get-aduser @adQry\r"
],
"description": "Perform AD User query from designated OU and filtered on Enabled users"
},
"Test":{
"prefix": "jjk - full",
"body": [
"\r",
"${1:jkav_freememory}"
]
},
"Active Subs":{
"prefix": "jjk - Active Subs",
"body" :[
"$${1:VariableName} = (Get-AzSubscription).Where{$$_.State -eq 'Enabled'}\r"
],
"description": "Creates an array of Axtive Azure Subscriptions"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment