Tuesday, January 21, 2020

Creating your OWN SNIPPETS



If you are utilizing ISE and want to add your handy Snippets, here is a template for you to use. Accessing snippets can be done, within ISE by pressing CTRL + J keys. From the selection, available snippets can be reached.


#(-)_.New-Snippet-Text
$SnippetText = @'

 #Place PS code here
  <CODE GOES HERE> 
}
'@

#(-)_.New-Snippet-TParams
$NewSnippetParams = @{
Title       = 'SNIPPET-NAME'
Description = 'SNIPPET-NAME-USAGE'
Text        = $SnippetText
CaretOffset = 33
Author      = 'Author NAME'
}

New-IseSnippet @NewSnippetParams


Some Examples

#(-)_.Foreach-Snippet-V1
$SnippetText = @'

#(-)_.Add Vars
$Path  = 'C:\Temp\List.TXT'
$List  = Get-Content -Path $path -ErrorAction Continue

if(!(Test-Path $Path ))
{
    Write-warning 'Import File cannot be located'
    Write-warning 'Script will stop'
    Start-Sleep -Seconds 5
    break;
}

#(-)_.Start Looping
foreach ($Item in $Items)
{
 Write-host 'Processing:'$Item -f DarkYellow
}

'@

$NewSnippetParams = @{
Title       = 'foreach-Snippet'
Description = 'foreach-Snippet'
Text        = $SnippetText
CaretOffset = 33
Author      = 'Casey.DeDeal'
}

New-IseSnippet @NewSnippetParams





# New-Snippet
$SnippetText = @'

function Function-Get-TimeStamp {
 
  [CmdletBinding()]
  param()  
  return "[{0:MM/dd/yy} {0:HH:mm:ss}]" -f (Get-Date)   
}


'@

$NewSnippetParams = @{
Title       = 'Get-TimeStamp'
Description = 'Get-TimeStamp Snippet'
Text        = $SnippetText
CaretOffset = 33
Author      = 'Casey DeDeal'
}

New-IseSnippet @NewSnippetParams


Casey, Dedeal
Principal Systems Engineer


No comments:

Post a Comment