Skip to content

Instantly share code, notes, and snippets.

@JohnL4
Last active September 1, 2023 19:43
Show Gist options
  • Save JohnL4/988ecd46e079c0aa2dd697d650486e8e to your computer and use it in GitHub Desktop.
Save JohnL4/988ecd46e079c0aa2dd697d650486e8e to your computer and use it in GitHub Desktop.
awk-like BEGIN and END processing of a pipeline in PowerShell; adding -Verbose and -WhatIf support via CmdletBinding
<#
.SYNOPSIS
Process pipeline junk
.NOTES
The mere presense of CmdletBinding() gives the -Verbose (-vb) parameter, so all Write-Verbose statement actually work.
Adding SupportsShouldProcess=$true gives -WhatIf, and all things this function/script does that can affect the system get turned
into verbose dry run ops.
#>
[CmdletBinding(SupportsShouldProcess=$True)] # Attribute goes on 'param' keyword.
param(
[Parameter( ValueFromPipeline=$true)]
$InputObject
)
BEGIN {
Write-Verbose "BEGIN"
}
PROCESS {
Write-Verbose "PROCESS"
}
END {
Write-Verbose "END"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment