Split-Array.ps1


<#PSScriptInfo
 
.VERSION 1.0
 
.GUID ff1598af-2d98-439d-a650-0b7525b77b6d
 
.AUTHOR saw-friendship
 
.COMPANYNAME
 
.COPYRIGHT
 
.TAGS
 
.LICENSEURI
 
.PROJECTURI
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
 
 
#>


<#
 
.DESCRIPTION
 Split Array to any parts for multithreads process
 
#>
 
    [CmdletBinding()]
    param(
        [ValidateRange(1,1000)][int]$Thread,
        [Parameter(ValueFromPipeline = $true)]$InputObject,
        [switch]$ExpandHashTable
    )

    Begin {
        $Count = $InputObject.count
        $i = 1
        $Hash = @{}
        1..$Thread | % {$Hash.Add($_,[array]@())}
    }
    Process {

        $InputObject | % {
        $Hash[$i] += $_
        if($i -eq $Thread){$i = 1}else{$i++}
        }
        
    }
    End {
        if(!$ExpandHashTable){$Hash}else{$Hash.Values}
    }