Skip to content

Instantly share code, notes, and snippets.

@rbleattler
Last active March 23, 2021 19:09
Show Gist options
  • Save rbleattler/5d95713c257840baf00f82259f4a082f to your computer and use it in GitHub Desktop.
Save rbleattler/5d95713c257840baf00f82259f4a082f to your computer and use it in GitHub Desktop.
A VSCode PowerShell snippet that builds an advanced function template with begin/process/end, and transcript logging.
# @prefix Function Advanced
# @description Advanced Function Block
function Write-MyFunction {
[CmdletBinding()]
param (
$$MyVariable1
)
begin {
Write-Debug "Enter [$($$PSCmdlet.MyInvocation.MyCommand.Name)]..."
$$PSBoundParameters.Keys | ForEach-Object {
if ($$PSBoundParameters.$$PSItem -is [string]) {
Write-Debug "[$($$PSCmdlet.MyInvocation.MyCommand.Name)] $$_ : $($$PSBoundParameters.Item($$_))"
} else {
Write-Debug "[$($$PSCmdlet.MyInvocation.MyCommand.Name)] $$_ : $($$PSBoundParameters.Item($$_).GetType())"
}
}
if ($$Transcript) {
if ([string]::IsNullOrWhiteSpace($$LogPath)) {
$$LogPath = $$ENV:Temp
}
$$ScriptName = $$PSCmdlet.MyInvocation.MyCommand.Name.Split('.ps')[0]
$$Now = Get-Date -Format yyyyMMdd_hh-mm-ss
$$ScriptLogName = '{0}.{1}.txt' -f $$Now, $$ScriptName
$$ScriptLog = Join-Path -Path $$LogPath -ChildPath $$ScriptLogName
Start-Transcript -Path $$ScriptLog
}
}
process {
#DO Something
${0:$TM_SELECTED_TEXT}
}
end {
if ($$Transcript) {
Stop-Transcript
}
Write-Debug "Exit [$($$PSCmdlet.MyInvocation.MyCommand.Name)]..."
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment