0

I have a Powershell script that retrieves some data from a database, uses Group-Object to collect it into similar groups, then uses foreach to act on each group.

The trouble is that some groups have more than 100 items, but my process can only act on 100 at once. It was written specifically to handle the items in the Group-Object results and I'd rather not rewrite that part.

Is there some elegant way to have Group-Object break up the groups into at most 100 items at a time? Or do I have to collect them with a for loop?

0

Here's what I came up with, partially thanks to this answer:

# create hashtable with unique basedomain as key and zero as value
$NameHash = @{};
foreach ($i in $tbl.rows | Group-Object basedomain) {
  $NameHash[$i.Name]=0;
}
# regroup using the hashtable as a counter source
$result = $tbl.rows | Group-Object basedomain, { [math]::Floor($NameHash[$_.basedomain]++ / 100) }
1
  • Actually, I simplified it further. Now the SQL that generates the data will itself use a hash function to put the members into bins. – Ross Presser Oct 29 '17 at 3:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.