Last active
November 4, 2023 23:11
-
-
Save trackd/95b2eaf78d57eabb9cf259c5e34632eb to your computer and use it in GitHub Desktop.
i made it better or worse, depending on your view 🤣
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using namespace System.Management.Automation.Language | |
function table2am { | |
<# | |
format-table sugar that knows not to format if you're assigning to a variable. | |
it will automatically switch to Select-Object when assigned. | |
also tries to figure out if it's the last command in the pipeline otherwise it will default to Select-Object. | |
only implemented Property param atm, just testing it out for fun. | |
Set-Alias -Name ft -Value table -Force | |
to override default ft alias you need -Force. | |
#> | |
[CmdletBinding()] | |
param( | |
[Parameter(Mandatory, ValueFromPipeline)] | |
[object] $InputObject, | |
[Parameter(Position = 0)] | |
[object[]] $Property, | |
[Switch] $AutoSize | |
) | |
begin { | |
$ast = ([Parser]::ParseInput($MyInvocation.Line, [ref]$null, [ref]$null)).EndBlock.Statements[0] | |
if ($ast.operator) { | |
# assigned to variable, use Select-Object | |
$PSBoundParameters.Remove('AutoSize') | |
$step = { Microsoft.PowerShell.Utility\Select-Object @PSBoundParameters }.GetSteppablePipeline() | |
} | |
else { | |
$lastcmd = $ast.PipelineElements[-1].commandElements[0].Value | |
$lastcmdInfo = $ExecutionContext.InvokeCommand.GetCommand($lastcmd,'All') | |
if ($MyInvocation.MyCommand -in @($lastcmdInfo,$lastcmdInfo.ResolvedCommand)) { | |
#last command in pipeline is format-table, proceed. | |
$step = { Microsoft.PowerShell.Utility\Format-Table @PSBoundParameters }.GetSteppablePipeline() | |
} | |
else { | |
<# | |
last command in the pipeline is something else, | |
someone is deliriously drunk and trying to format the output while piping to other commands | |
Use Select-Object. | |
#> | |
Write-Debug "You are drunk, goto bed, you cant pipe Format-Table to $lastcmdInfo / $( $lastcmdInfo.ResolvedCommand )" | |
$PSBoundParameters.Remove('AutoSize') | |
$step = { Microsoft.PowerShell.Utility\Select-Object @PSBoundParameters }.GetSteppablePipeline() | |
} | |
} | |
$step.Begin($PSCmdlet) | |
} | |
process { | |
$step.Process($InputObject) | |
} | |
end { | |
$step.End() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment