Powershell 如何获取管道对象的计数?我不知道';我不想把管道累加到缓冲区
假设我有一些powershell代码:Powershell 如何获取管道对象的计数?我不知道';我不想把管道累加到缓冲区,powershell,count,pipe,Powershell,Count,Pipe,假设我有一些powershell代码: function count-pipe { [CmdletBinding()] param ( [Parameter(ValueFromPipeline=$true)] [object[]]$inputObject ) process { $PipeCount = <# How to get count of the pipe? Expect: #> 5
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 | % { $_ }
函数计数管道{
[CmdletBinding()]
param(
[参数(ValueFromPipeline=$true)]
[object[]]$inputObject
)
过程{
$PipeCount=5
写入输出$inputObject#路径抛出
}
}
1..5 |计数管道|%{$}
是的,我可以将计数求和/度量为temp变量,并使用tee cmdlet。我认为这个解决方案使管道暂停。我认为temp valiable也不是解决内存消耗的好方法
我可以在不累加到临时变量的情况下获得管道对象计数吗
谢谢。我认为使用计数器变量是我会使用的解决方案:
function count-pipe {
[CmdletBinding()]
param (
[Parameter(ValueFromPipeline=$true)]
[object[]]$InputObject
)
Process {
$PipeCount++
$_
}
End {
Write-Verbose $PipeCount
}
}
'a','b' | count-pipe -verbose | % { $_ }
找到替代答案:
使用
Powershell:
@(1,2,3)|测量对象
输出:
计数:3
平均值:
总数:
最大值:
最低要求:
财产:
Powershell:
1..5 |测量对象-所有统计信息
输出:
计数:5
平均:3
总数:15
最多:5
最低限额:1
标准偏差:1.58113883008419
财产:
对于此用例: Powershell:
$listmasure=$list |度量对象
$listCount=$listMeasure.Count
和中的示例您打算如何使用
$PipeCount
,为什么$PipeCount++
不是一个解决方案?管道的要点是,一次只能有一个对象。那你是在预测未来吗?不知道没有临时工你会怎么做variable@MarkWragg,谢谢。代码在管道完成后获取管道计数。问题是:如何在进程内获取pipe.count?)@gms0ulman,是的。此评论是对先前版本的回答。对于此版本,pipe.count的值是正确的(cmdlet运行时)),问题是:如何在进程内获取pipe.count?如何在进程内获取管道计数?您不需要这样做,因为当管道中的每个项目“到达”时,进程对每个项目运行一次。要对项目进行计数,可以事先收集所有项目并迭代收集(如foreach
语句),也可以使用计数器(如本答案中所示)。