Splitting an Array in smaller arrays

by the_hutch at 2012-11-13 13:27:34

I have a utility that determines the number of pingable IPs in each of our network ranges and puts them in a text file. I’ve used these IPs to create several arrays.

$Range1 = Get-Content .\Range1.txt
$Range2 = Get-Content .\Range2.txt
$Range3 = Get-Content .\Range3.txt
$Range4 = Get-Content .\Range4.txt

Each range usually contains over 20,000 pingable IPs. So running a script against each individual range takes some serious time. I’d like to divide each array into several smaller arrays to run several background jobs in parallel. But I’m not sure the best way to go about doing this. Any thoughts? Thanks in advance.
by nohandle at 2012-11-14 07:15:51
Hi, this is my approach to tackle the problem.

the array is split to smaller arrays and output
if you capture the output you will get array of arrays

powershell does return only 10 values if 1…100 is specified on an array of length 10 and no errors. so I don`t have to split the array precisely

$example = 1…11

function Split-Array ([object]$InputObject,[int]$SplitSize=100)
for ($Index = 0; $Index -lt $length; $Index += $SplitSize)
#, encapsulates result in array
#-1 because we index the array from 0
$result= Split-Array -InputObject $example -SplitSize 10

Great and simple tool! Works like a charm. Thanks for posting!