本文介绍了如何获得管道对象的数量?我不想积累管道来缓冲的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有一些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 | % { $_ }
更多推荐
如何获得管道对象的数量?我不想积累管道来缓冲
发布评论