Skip to content

Instantly share code, notes, and snippets.

@beatcracker
Created February 4, 2017 15:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save beatcracker/0e21926541e607f2d196f99e4d35ab55 to your computer and use it in GitHub Desktop.
Save beatcracker/0e21926541e607f2d196f99e4d35ab55 to your computer and use it in GitHub Desktop.
Visualizing PowerShell pipeline
<#
.Synopsis
Visualize pipeline lifecycle and Begin/Process/End execution order for chained cmdlets.
.Description
Provides grafical view of the Begin/Process/End blocks execution order for nested pipeline.
.Parameter Pipes
Number of nested pipeleine functions.
.Parameter Items
Number of items send through the pipeline. Controls how many times Process blocks will be executed.
.Parameter BreakAfter
Stop pipeline processing after N's nested function. Simulates pipeline running out of items to process in the middle.
In real life this could happen if the pipeline includes Where-Object in the middle and all items fail to meet the "true" criteria.
.Parameter NoBegin
Generate functions without Begin block.
.Parameter NoProcess
Generate functions without Process block.
.Parameter NoEnd
Generate functions without End block.
.Example
View-Pipeline
Generate and execute single advanced function with Begin/Process/End blocks: View-Pipeline-1
.Example
View-Pipeline -Pipes 3
Generate and execute 3 advanced functions with Begin/Process/End blocks: View-Pipeline-1 | View-Pipeline-2 | View-Pipeline-3
.Example
View-Pipeline -Items 2
Generate and execute single advanced function with Begin/Process/End blocks.
Process block will be executed twice, for each of the piped items.
.Example
View-Pipeline -Pipes 2 -NoBegin
Generate and execute 2 advanced functions with Process/End blocks only: View-Pipeline-1 | View-Pipeline-2
.Notes
Author: beatcracker (http://beatcracker.wordpress.com, https://github.com/beatcracker)
License: Microsoft Public License (http://opensource.org/licenses/MS-PL)
.Link
https://beatcracker.wordpress.com/2017/02/04/visualizing-powershell-pipeline/
#>
function View-Pipeline {
Param (
[uint32]$Pipes = 1,
[uint32]$Items = 1,
[uint32]$BreakAfter = 0,
[switch]$NoBegin,
[switch]$NoProcess,
[switch]$NoEnd
)
if ($BreakAfter) {++$BreakAfter}
1..$Pipes | ForEach-Object -Begin {
$Expression = @()
} -Process {
'function {3}-{0} {{
[CmdletBinding()]
Param (
[Parameter(ValueFromPipeline = $true)]
$InputObject
)
{4} Begin {{"[{{0}}]::Begin" -f $MyInvocation.MyCommand.Name | Write-Host -ForegroundColor Green}}
{5}<#
Process {{
if ({0} -eq {1}) {{return}}
"{{0}}[{{1}}]::Process" -f ("`t"*{0}), $MyInvocation.MyCommand.Name | Write-Host -ForegroundColor Yellow
"{{0}}In : `"{{1}}`"" -f ("`t "*{0}), $InputObject | Write-Host -ForegroundColor DarkYellow
"{{0}}Out: `"{{1}}`"" -f ("`t "*{0}), $MyInvocation.MyCommand.Name | Write-Host -ForegroundColor DarkYellow
@($MyInvocation.MyCommand.Name) * {2}
}}
#>
{6} End {{"[{{0}}]::End" -f $MyInvocation.MyCommand.Name | Write-Host -ForegroundColor Red}}
}}' -f $_, $BreakAfter, $Items, $MyInvocation.MyCommand.Name, ('#'*[bool]$NoBegin), ('#'*![bool]$NoProcess), ('#'*[bool]$NoEnd) | Invoke-Expression
$Expression += $MyInvocation.MyCommand.Name + "-$_"
} -End {
($Expression -join ' | ') | ForEach-Object {
$_, ''| Write-Host -ForegroundColor Magenta
$_ + ' > $null' | Invoke-Expression
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment