Yevrag35

My PowerShell Snippets (VSCode)

Jul 16th, 2020
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JSON 8.24 KB | None | 0 0
  1. {
  2.     // Place your snippets for powershell here. Each snippet is defined under a snippet name and has a prefix, body and
  3.     // description. The prefix is what is used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
  4.     // $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders. Placeholders with the
  5.     // same ids are connected.
  6.     // Example:
  7.     // "Print to console": {
  8.     //  "prefix": "log",
  9.     //  "body": [
  10.     //      "console.log('$1');",
  11.     //      "$2"
  12.     //  ],
  13.     //  "description": "Log output to console"
  14.     // }
  15.     "Captured ForEach": {
  16.         "prefix": "foreach-capture",
  17.         "description": "A captured foreach loop.",
  18.         "body": [
  19.             "\\$all$1 = foreach (\\$${2:item} in \\$${1:items}) {",
  20.             "\t$0",
  21.             "}"
  22.         ]
  23.     },
  24.     "Comment Block (Better)": {
  25.         "prefix": "comment-help-better",
  26.         "description": "A better 'comment-based' help snippet.",
  27.         "body": [
  28.             "<#",
  29.             "\t.SYNOPSIS",
  30.             "\t\t${1:synopsis}",
  31.             "\t.DESCRIPTION",
  32.             "\t\t${2:description}",
  33.             "\t",
  34.             "\t.PARAMETER ${3:parameter}",
  35.             "\t\t${4:parameter1}",
  36.             "\t",
  37.             "\t.INPUTS",
  38.             "\t\t${5:Does not accept pipeline input.}",
  39.             "\t.OUTPUTS",
  40.             "\t\t${6:None.}",
  41.             "\t",
  42.             "\t.EXAMPLE",
  43.             "\t\t${7:example}",
  44.             "\t",
  45.             "\t.NOTES",
  46.             "\t\t${8:notes}",
  47.             "#>"
  48.         ]
  49.     },
  50.     "Current Directory Variable": {
  51.         "prefix": "curDir-p2",
  52.         "description": "Creates a variable to define the current directory of the running script.",
  53.         "body": [
  54.             "if (\\$null -eq (Get-Variable -Name 'PSScriptRoot' -ErrorAction 'SilentlyContinue')) {",
  55.             "\t\\$PSScriptRoot = Split-Path -Path \\$MyInvocation.MyCommand.Definition -Parent",
  56.             "}",
  57.             "$0"
  58.         ]
  59.     },
  60.     "Dynamic Parameter": {
  61.         "prefix": "dynamic-parameter",
  62.         "description": "Create a basic framework for DynamicParameters",
  63.         "body": [
  64.             "[CmdletBinding()]",
  65.             "param()",
  66.             "DynamicParam",
  67.             "{",
  68.             "\t# Use this block for defining the ValidateSet variables\r\n\r\n",
  69.             "\t#######################################################",
  70.             "\t\\$pName = \"${1:ParameterName}\";",
  71.             "\t\\$attCol = New-Object 'System.Collections.ObjectModel.Collection[System.Attribute]';",
  72.             "\t\\$pAtt = New-Object System.Management.Automation.ParameterAttribute -Property @{",
  73.             "\t\tMandatory = \\$true",
  74.             "\t\tPosition = 0",
  75.             "\t};",
  76.             "\t\\$attCol.Add(\\$pAtt);",
  77.             "\t\\$alias = New-Object System.Management.Automation.AliasAttribute(\"\");",
  78.             "\t\\$attCol.Add(\\$alias);",
  79.             "\t\\$valSet = \"$2\"# Array",
  80.             "\t\\$attCol.Add(\\$valSet);",
  81.             "\t\\$rtParam = New-Object System.Management.Automation.RuntimeParameter(\\$pName, \\$([${3:Type}]), \\$attCol);",
  82.             "\t\\$rtDict = New-Object System.Management.Automation.RuntimeParameterDictionary;",
  83.             "\t\\$rtDict.Add(\\$pName, \\$rtParam);",
  84.             "}",
  85.             "Begin",
  86.             "{",
  87.             "\t\\$${1} = \\$PSBoundParameters[\"${1}\"];",
  88.             "}",
  89.             "Process",
  90.             "{\r\n",
  91.             "}"
  92.         ]
  93.     },
  94.     "For Loop - WriteProgress": {
  95.         "prefix": "for-write-progress",
  96.         "description": "My 'for' loop.",
  97.         "body": [
  98.             "for (\\$${1:i} = ${2:1}; \\$$1 -${3:le} \\$${4:array}.${5:Count}; \\$$1++) {\r\n",
  99.             "\t\\$${6:item} = \\$$4[\\$$1 - 1]",
  100.             "\t\\$${7:progArgs} = @{",
  101.             "\t\tActivity = \"${8:Processing $6s}\"",
  102.             "\t\tStatus = \"Processing $6 \\$$1/\\$(\\$$4.$5)...$9\"",
  103.             "\t\tId = ${10:0}",
  104.             "\t\tPercentComplete = ((\\$$1/\\$(\\$$4.Count))*100)",
  105.             "\t}\r\n",
  106.             "\tWrite-Progress @progArgs\r\n",
  107.             "\t$0",
  108.             "}"
  109.         ]
  110.     },
  111.     "Function No-Param": {
  112.         "prefix": "func-noparam",
  113.         "description": "A 'Function' block with no param block.",
  114.         "body": [
  115.             "Function ${1:functionName}($0) {",
  116.             "\t",
  117.             "}"
  118.         ]
  119.     },
  120.     "Multi-line String": {
  121.         "prefix": "multi-line-string",
  122.         "description": "Creates a new variable for a 'here'-string.",
  123.         "body": [
  124.             "\\$${1:line} = @'",
  125.             "$0",
  126.             "$2'@"
  127.         ]
  128.     },
  129.     "New Object": {
  130.         "prefix": "newo",
  131.         "description": "New-Object snippet",
  132.         "body": "New-Object -TypeName \"$0\""
  133.     },
  134.     "New Object - ArgumentList": {
  135.         "prefix": "newo-arg",
  136.         "description": "New Object with Argument List snippet",
  137.         "body": "New-Object -TypeName \"$1\" -ArgumentList $0"
  138.     },
  139.     "New Script Layout": {
  140.         "prefix": "new-script-layout",
  141.         "description": "Creates the layout of a self-contained script with regions denoting each section",
  142.         "body": [
  143.             "<#\r\n\t.SYNOPSIS\r\n\t\t\r\n",
  144.             "\t.DESCRIPTION",
  145.             "\t\t\r\n\t\t\r\n\t\tAuthor:\tMike Garvey\r\n\t\tDate:\t$CURRENT_MONTH/$CURRENT_DATE/$CURRENT_YEAR_SHORT\r\n",
  146.             "\t.EXAMPLE\r\n\t\t\r\n",
  147.             "\t.PARAMETER \r\n\t\t\r\n",
  148.             "#>",
  149.             "[CmdletBinding()]",
  150.             "param",
  151.             "(",
  152.             "\t",
  153.             ")\r\n",
  154.             "#region ---- FUNCTIONS ----\r\n",
  155.             "Function $0()\r\n{",
  156.             "\t\r\n}\r\n",
  157.             "\r\n\r\n#endregion\r\n\r\n",
  158.             "#region ---- PRE-PROCESSING ----\r\n\r\n\r\n#endregion\r\n\r\n",
  159.             "#region ---- SCRIPT BODY ----\r\n\r\n\r\n#endregion\r\n"
  160.         ]
  161.     },
  162.     "Out-String Object": {
  163.         "prefix": "out-string-obj",
  164.         "description": "Pipes the specified object to an Out-String.",
  165.         "body": "\\$(\\$$1 | Out-String)"
  166.     },
  167.     "Parameter-String": {
  168.         "prefix": "param-string",
  169.         "description": "Creates a string-type function parameter.",
  170.         "body": [
  171.             "[Parameter(Mandatory=\\$${1:false})]",
  172.             "[string] \\$$0"
  173.         ]
  174.     },
  175.     "Parameter-Typeless": {
  176.         "prefix": "param-typeless",
  177.         "description": "Creates a type-less (to be defined) function parameter.",
  178.         "body": [
  179.             "[Parameter(Mandatory=\\$${1:false})]",
  180.             "[${2:object}] \\$$0"
  181.         ]
  182.     },
  183.     "PSBoundParameter - ContainsKey": {
  184.         "prefix": "psb-contains",
  185.         "description": "Adds an \"if (\\$PSBoundParameters.ContainsKey()\" positive condition block",
  186.         "body": [
  187.             "if (\\$PSBoundParameters.ContainsKey(\"$1\"))",
  188.             "{",
  189.             "\t$0",
  190.             "}"
  191.         ]
  192.     },
  193.     "PSBoundParameters - NotContainsKey": {
  194.         "prefix": "psb-nocontains",
  195.         "description": "Adds an \"if (\\$PSBoundParameters.ContainsKey()\" negative condition block",
  196.         "body": [
  197.             "if (-not \\$PSBoundParameters.ContainsKey(\"$1\"))",
  198.             "{",
  199.             "\t$0",
  200.             "}"
  201.         ]
  202.     },
  203.     "PSBoundParameters - ContainsKey ElseIf": {
  204.         "prefix": "psb-contains-elseif",
  205.         "description": "Adds an \"elseif (\\$PSBoundParameters.ContainsKey()\" positive condition block",
  206.         "body": [
  207.             "elseif (\\$PSBoundParameters.ContainsKey(\"$1\"))",
  208.             "{",
  209.             "\t$0",
  210.             "}"
  211.         ]
  212.     },
  213.     "PSBoundParameters - NotContainsKey ElseIf": {
  214.         "prefix": "psb-nocontains-elseif",
  215.         "description": "Adds an \"elseif (\\$PSBoundParameters.ContainsKey()\" negative condition block",
  216.         "body": [
  217.             "elseif (-not \\$PSBoundParameters.ContainsKey(\"$1\"))",
  218.             "{",
  219.             "\t$0",
  220.             "}"
  221.         ]
  222.     },
  223.     "Script CmdletBinding": {
  224.         "prefix": "param",
  225.         "description": "Create the CmdletBinding and Param block for a PSScript file.",
  226.         "body": [
  227.             "[CmdletBinding(SupportsShouldProcess=\\$${1:false}, ConfirmImpact=\"${2:None}\")]",
  228.             "param (",
  229.             "\t$0",
  230.             ")",
  231.             "Begin {",
  232.             "\t",
  233.             "}",
  234.             "Process {",
  235.             "\t",
  236.             "}"
  237.         ]
  238.     },
  239.     "String IsNullOrEmpty": {
  240.         "prefix": "string-nullorempty",
  241.         "description": "String is null or empty",
  242.         "body": "[string]::IsNullOrEmpty($0)"
  243.     },
  244.     "String NOT IsNullOrEmpty": {
  245.         "prefix": "string-not-nullorempty",
  246.         "description": "String is NOT null or empty.",
  247.         "body": "-not [string]::IsNullOrEmpty($0)"
  248.     },
  249.     "String IsNullOrWhitespace": {
  250.         "prefix": "string-nullorwhitespace",
  251.         "description": "String is null, empty, or whitespace",
  252.         "body": "[string]::IsNullOrWhitespace($0)"
  253.     },
  254.     "String NOT IsNullOrWhitespace": {
  255.         "prefix": "string-not-nullorwhitespace",
  256.         "description": "String is NOT null, empty, or whitespace",
  257.         "body": "-not [string]::IsNullOrWhitespace($0)"
  258.     },
  259.     "Write Host Double With Colour": {
  260.         "prefix": "write-host-double",
  261.         "description": "Write-Host (double quotes) with ForegroundColor specified.",
  262.         "body": "Write-Host \"$0\" -ForegroundColor ${1|Black,Blue,Cyan,DarkBlue,DarkCyan,DarkGray,DarkGreen,DarkMagenta,DarkRed,DarkYellow,Gray,Green,Magenta,Red,White,Yellow|}"
  263.     },
  264.     "Write Host Single With Colour": {
  265.         "prefix": "write-host-single",
  266.         "description": "Write-Host (single quotes) with ForegroundColor specified.",
  267.         "body": "Write-Host '$0' -ForegroundColor ${1|Black,Blue,Cyan,DarkBlue,DarkCyan,DarkGray,DarkGreen,DarkMagenta,DarkRed,DarkYellow,Gray,Green,Magenta,Red,White,Yellow|}"
  268.     }
  269. }
Add Comment
Please, Sign In to add comment