PoshCode Archive  Artifact [9592f9fd74]

Artifact 9592f9fd745c9ca6ff80c227194cd3d5cd1595ab91341a2f3067c25d0310c60b:

  • File out-default.ps1 — part of check-in [892d1c6f98] at 2018-06-10 12:56:39 on branch trunk — Problem: Type in a command to show the output, then wish you had saved it to a variable (user: unknown size: 1849)

# encoding: ascii
# api: powershell
# title: 
# description: Problem: Type in a command to show the output, then wish you had saved it to a variable
# version: 0.1
# type: function
# license: CC0
# function: out-default
# x-poshcode-id: 1210
# x-archived: 2009-07-22T11:40:41
#
# Solution: Save the last 10 objects that are output into a special variable.
# Implementation: put this function in your profile.  change the 10 in the script to the number you like.
#
function out-default() {
[CmdletBinding()]
param(
    [Parameter(ValueFromPipeline=$true)]
    [System.Management.Automation.PSObject]
    ${InputObject})

begin
{
    
    try {
        $outBuffer = $null
        if ($PSBoundParameters.TryGetValue('OutBuffer', [ref]$outBuffer) -and $outBuffer -gt 1024)
        {
            $PSBoundParameters['OutBuffer'] = 1024
        }
        $wrappedCmd = $ExecutionContext.InvokeCommand.GetCommand('Out-Default', [System.Management.Automation.CommandTypes]::Cmdlet)
        $scriptCmd = {& $wrappedCmd @PSBoundParameters }
        $steppablePipeline = $scriptCmd.GetSteppablePipeline()
        $steppablePipeline.Begin($PSCmdlet)
    } catch {
        throw
    }
}

process
{
    try {
        $steppablePipeline.Process($_)
        if ($last_output -eq $null)
        {
            $last_output = @()
        }
        if ($last_output.Length -lt 10)
        {
            $last_output += $_
        }
        else
        {
            $null, $last_output = $last_output
            $last_output += $_
        }
        
    } catch {
        throw
    }
}

end
{
    try {
        $steppablePipeline.End()
        $global:last_output = $last_output
    } catch {
        throw
    }
}
<#

.ForwardHelpTargetName Out-Default
.ForwardHelpCategory Cmdlet

#>

}