gpt4 book ai didi

powershell - 缓冲管道输入到 PowerShell cmdlet 的设计模式

转载 作者:行者123 更新时间:2023-12-04 15:40:51 24 4
gpt4 key购买 nike

我偶尔会遇到支持管道输入到 Cmdlet 的情况,但我希望执行的操作(例如数据库访问)对合理数量的对象进行批处理是有意义的。

实现这一目标的典型方法如下所示:

function BufferExample {
<#
.SYNOPSIS
Example of filling and using an intermediate buffer.
#>
[CmdletBinding()]
param(
[Parameter(ValueFromPipeline)]
$InputObject
)

BEGIN {
$Buffer = New-Object System.Collections.ArrayList(10)
function _PROCESS {
# Do something with a batch of items here.
Write-Output "Element 1 of the batch is $($Buffer[1])"
# This could be a high latency operation such as a DB call where we
# retrieve a set of rows by primary key rather than each one individually.

# Then empty the buffer.
$Buffer.Clear()
}
}

PROCESS {
# Accumulate the buffer or process it if the buffer is full.
if ($Buffer.Count -ne $Buffer.Capacity) {
[void]$Buffer.Add($InputObject)
} else {
_PROCESS
}
}

END {
# The buffer may be partially filled, so let's do something with the remainder.
_PROCESS
}
}

有没有更少的“样板”方法来做到这一点?

一种方法可能是编写我在此处称为“_PROCESS”的函数以接受数组参数但不接受管道输入,然后将向用户公开的 cmdlet 作为代理函数构建以缓冲输入并传递缓冲区如中所述
Proxy commands .

或者,我可以在我希望编写的 cmdlet 主体中添加动态源代码以支持此功能,但这似乎容易出错并且可能难以调试/理解。

最佳答案

我从您的示例中制作了一个可重复使用的 cmdlet。

function Buffer-Pipeline {
[CmdletBinding()]
param(
$Size = 10,
[Parameter(ValueFromPipeline)]
$InputObject
)
BEGIN {
$Buffer = New-Object System.Collections.ArrayList($Size)
}

PROCESS {
[void]$Buffer.Add($_)

if ($Buffer.Count -eq $Size) {
$b = $Buffer;
$Buffer = New-Object System.Collections.ArrayList($Size)
Write-Output -NoEnumerate $b
}
}

END {
# The buffer may be partially filled, so let's do something with the remainder.
if ($Buffer.Count -ne 0) {
Write-Output -NoEnumerate $Buffer
}
}
}

用法:
@(1;2;3;4;5) | Buffer-Pipeline -Size 3 | % { "$($_.Count) items: ($($_ -join ','))" }

输出:
3 items: (1,2,3)
2 items: (4,5)

另一个例子:
1,2,3,4,5 | Buffer-Pipeline -Size 10 | Measure-Object

Count : 2

处理每批:
 1,2,3 | Buffer-Pipeline -Size 2 | % { 
$_ | % { "Starting batch of $($_.Count)" } { $_ * 100 } { "Done with batch of $($_.Count)" }
}

Starting batch of 2
100
200
Done with batch of 2
Starting batch of 1
300
Done with batch of 1

关于powershell - 缓冲管道输入到 PowerShell cmdlet 的设计模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41465646/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com