I am cloning VMs on ESX server from template. Simplified code looks like this:
Workflow Create-VM {
$List = 1..500
foreach -parallel ($Elem in $List)
{
A trivial solution is to divide the list into smaller chunks and use that as input for parallel foreach. Like so,
Workflow Create-VM {
$List = 1..500
# Calclulate indexes to get elements 0,3; 4,7; 8,11 and so on
# Use the .. operator to extract those elements from $list and pass
# 'em to foreach -parallel processing part
for($i=0;$i -le $List.Count-4; $i+=4) {
foreach -parallel ($Elem in $list[$i..($i+3)]) {
# Create VM ...
# Configure created VM ..
}
}
}