如何获得管道对象的数量?我不想积累管道来缓冲 [英] How to get count of pipe objects? I don't want accumulate pipe to buffer

查看:61
本文介绍了如何获得管道对象的数量?我不想积累管道来缓冲的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一些powershell代码:

 函数计数管道{
[CmdletBinding()]
param(
[Parameter(ValueFromPipeline = $ true)]
[object []] $ inputObject


进程{
$ PipeCount = <#如何获取管道数量?期望:#> 5

写输出$ inputObject#路径抛出
}
}

1..5 |计数管| %{$ _}

是的,我可以对温度变量求和/测量并使用三通cmdlet。我认为此解决方案使管道暂停。我认为临时变量也不是解决内存消耗问题的好方法。

谢谢。

解决方案

我认为使用计数器变量是我会使用的解决方案:

 函数计数管道{
[CmdletBinding()]
param(
[参数(ValueFromPipeline = $ true)]
[object []] $ InputObject

进程{
$ PipeCount ++
$ _
}
结尾{
写详细$ PipeCount
}

}

'a','b'|计数管详细%{$ _}


Assume I have some powershell code:

function count-pipe {
    [CmdletBinding()]
    param (
        [Parameter(ValueFromPipeline=$true)]
        [object[]]$inputObject
    )

    process {
        $PipeCount = <# How to get count of the pipe? Expect: #> 5

        Write-Output $inputObject # path-throw
    }
}

1..5 | count-pipe | % { $_ }

Yes, I can sum/measure count to temp-variable and use the tee cmdlet. I think this solution make pause the pipe. I think also the temp-valiable is not a good solution relate a memory consumption.

Can I get a pipe objects count without accumulate to temp-variable?

Thanks.

解决方案

I think using a counter variable is the solution I would use:

function count-pipe {
    [CmdletBinding()]
    param (
        [Parameter(ValueFromPipeline=$true)]
        [object[]]$InputObject
    )
    Process {
        $PipeCount++
        $_
    }
    End {
        Write-Verbose $PipeCount
    }

}

'a','b' | count-pipe -verbose | % { $_ }

这篇关于如何获得管道对象的数量?我不想积累管道来缓冲的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆