如何获得管道对象的数量?我不想积累管道来缓冲

编程入门 行业动态 更新时间:2024-10-24 04:32:58
本文介绍了如何获得管道对象的数量?我不想积累管道来缓冲的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

假设我有一些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 | % { $_ }

更多推荐

如何获得管道对象的数量?我不想积累管道来缓冲

本文发布于:2023-07-07 22:17:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1068542.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:我不想   道来   如何获得   管道   对象

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!