PowerShell函数,用于将管线对象分块到阵列can';我得不到正确的结果

PowerShell函数,用于将管线对象分块到阵列can';我得不到正确的结果,powershell,pipeline,Powershell,Pipeline,我正在学习PowerShell,并尝试编写一个函数将管线对象分块到数组中。如果用户提供了scriptblock$进程,则函数将在将每个管线对象发送到管线之前将scriptblock应用于每个管线对象(在下面的代码中尚未实现)。假设将参数$InputObject设置为1,2,3,4,5,将$ElementsPerChunk设置为2,那么函数应该返回3个数组@(1,2),@(3,4),@(5)。以下是我目前的代码: function Chunk-Object { [CmdletBinding

我正在学习PowerShell,并尝试编写一个函数将管线对象分块到数组中。如果用户提供了scriptblock$进程,则函数将在将每个管线对象发送到管线之前将scriptblock应用于每个管线对象(在下面的代码中尚未实现)。假设将参数$InputObject设置为
1,2,3,4,5
,将$ElementsPerChunk设置为
2
,那么函数应该返回3个数组
@(1,2),@(3,4),@(5)
。以下是我目前的代码:

function Chunk-Object
{
    [CmdletBinding()]
    Param (
        [Parameter(Mandatory = $true,
                   ValueFromPipeline = $true,
                   ValueFromPipelineByPropertyName = $true)] [object[]] $InputObject,
        [Parameter()] [scriptblock] $Process,
        [Parameter()] [int] $ElementsPerChunk
    )

    Begin {
        $cache = @();
        $index = 0;
    }
    Process {
        if($cache.Length -eq $ElementsPerChunk) {
            # if we collected $ElementsPerChunk elements in an array, sent it out to the pipe line
            Write-Output $cache;

            # Then we add the current pipe line object to the array and set the $index as 1
            $cache = @($_);
            $index = 1;
        }
        else {
            $cache += $_;
            $index++;
        }

    }
    End {
        # Here we check if there are anything still in $cache, if so, just sent out it to pipe line
        if($cache) {
            Write-Output $cache;
        }
    }
}


echo 1 2 3 4 5 6 7 | Chunk-Object -ElementsPerChunk 2;
Write-Host "=============================================================================";
(echo 1 2 3 4 5 6 7 | Chunk-Object -ElementsPerChunk 2).gettype();
Write-Host "=============================================================================";
(echo 1 2 3 4 5 6 7 | Chunk-Object -ElementsPerChunk 2).length;
当我执行代码时,我得到:

1
2
3
4
5
6
7
=============================================================================

IsPublic IsSerial Name                                     BaseType                                                                                                                                          
-------- -------- ----                                     --------                                                                                                                                          
True     True     Object[]                                 System.Array                                                                                                                                      
=============================================================================
7

如您所见,结果是一个数组,包含7个元素。我期望从结果中得到4个元素:
@(1,2),@(3,4),@(5,6),@(7)
。有人能帮我检查代码并解释问题吗?谢谢。

问题在于,当您从进程块返回块数组时,管道正在“展开”每个块数组。尝试在返回数组的进程块中进行此更改:

Write-Output (,$cache);

这将使返回成为2D数组,然后它将“展开”到单个数组中,而不是单个数组元素。

它不是2D数组,而是数组本身已放入单个元素数组中。事实上,我认为你的代码应该可以工作,但还没有经过测试。然而,我想知道这个“特性”是在哪里描述的,你是怎么知道的?我尝试搜索PowerShell规范版本2和版本3,但找不到相应的说明。非常感谢。$cache的第一个元素是$cache[0]。(,$cache)的相同元素将是(,$cache)[0][0]。也许“2D”这个词并不完全正确,但如果不是,我不确定它应该是什么。我必须仔细阅读规范才能找到它的确切位置,但我从Monad B3版本的经验中知道这一点。