Get-BatchSet.ps1

<#PSScriptInfo
 
.VERSION 1.0
 
.GUID fab1d354-625d-4554-a81a-df732c59249d
 
.AUTHOR atorrest
 
.COMPANYNAME atorrest
 
.COPYRIGHT atorrest
 
.DESCRIPTION
    Split a big set into smaller sets.
 
#>

function Get-BatchSet {
<#
    .SYNOPSIS
        Split a big set into smaller sets.
     
    .DESCRIPTION
        Split a big set into smaller sets. Each set will have the size of $BatchSize
     
    .PARAMETER BatchSize
        Number elements to include in each set.
     
    .PARAMETER TotalSize
        Total number elements in the set.
     
    .EXAMPLE
        PS C:\> Get-BatchSet -BatchSize 30 -TotalSize 114
         
        Set Start End
        --- ----- ----
           1 1 30
           2 31 60
           3 61 90
           4 91 114
 
#>
    
    [CmdletBinding()]
    [OutputType([PSCustomObject])]
    param
    (
        [Parameter(Mandatory = $true)]
        [ValidateNotNull()]
        [int]
        $BatchSize,
        [Parameter(Mandatory = $true)]
        [ValidateNotNull()]
        [int]
        $TotalSize
    )
    
    $StartRow = 1
    $BatchIndex = 0
    
    For ($Index = 0; $Index -lt $TotalSize; $Index += $BatchSize) {
        $BatchIndex++
        [PSCustomObject] @{
            Set = $BatchIndex;
            Start = ($Index + 1);
            End = ([System.Math]::Min(($BatchIndex * $BatchSize), $TotalSize))
        } | Write-Output
    }
}