Last active
October 11, 2018 14:27
-
-
Save GenesisCoast/66dd5f31b8c685d301da59f1d67e25bd to your computer and use it in GitHub Desktop.
Example of how to batch process an array using a PowerShell pipeline.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Implementation | |
function Batch-Pipe { | |
[CmdletBinding()] | |
[OutputType('System.String')] | |
param( | |
[Parameter(Mandatory = $true, ValueFromPipeline = $true)] | |
[ValidateNotNull()] | |
$InputArray | |
) | |
begin { | |
# Intialize the batch variable. | |
New-Variable -Name '_batch' -Scope 'Local' | |
} | |
process { | |
# Process acts as a ForEach Loop so we need to batch it. | |
$local:_batch += $InputArray | |
} | |
end { | |
# Now $InputArray is batched we can process the array. | |
return $local:_batch | |
} | |
} | |
# Usage, returns 'One Two' | |
@( | |
'One', | |
'Two' | |
) | Batch-Pipe |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment