Experimenting some more, I came across a scenario when the number of computer increases from 18 to over 2000, where I got the following error:
The operation did not complete within the allotted timeout of 00:00:30. The time allotted to this operation may have
been a portion of a longer timeout.
At Test-MultiConnection:10 char:10
I searched the web and found this article: WorkflowServiceHost: Limitation in the number of running instance?
I also found a reference to a no-longer-existing article by someone who wrote a “Foreach-Parallel” cmdlet. I think the correct way to deal with this is via the New-PSWorkflowSession cmdlet where you can customize throttle limit, concurrent sessions, etc.
I couldn’t figure out how to apply the settings to a specific workflow (maybe it affects the current PS session all together?), so I wrote a little function/cmdlet that I’ve found myself needing from time to time. This won’t perform well on large collections due to, among other things, array concatenation, but should be fine for many purposes. I probably should have figured out the correct way, but now I’m posting it anyway.
I called it Split-Collection and the code looks like this:
function Split-Collection {
[CmdletBinding()]
param(
[Parameter(ValueFromPipeline=$true)] $Collection,
[Parameter(Mandatory=$true)][ValidateRange(1, 247483647)][int] $ChunkSize
)
begin {
$Ctr = 0
$Arrays = @()
$TempArray = @()
}
process {
if (++$Ctr -eq $ChunkSize) {
$Ctr = 0
$Arrays += , @($TempArray + $_)
$TempArray = @()
return
}
$TempArray += $_
}
end {
if ($TempArray) { $Arrays += , $TempArray }
$Arrays
}
}
Its behavior can be demonstrated like this:
PS C:\PowerShell> 1..10 | Split-Collection -ChunkSize 2 | %{ $_ -join ', ' }
1, 2
3, 4
5, 6
7, 8
9, 10
PS C:\PowerShell> 1..10 | Split-Collection -ChunkSize 3 | %{ $_ -join ', ' }
1, 2, 3
4, 5, 6
7, 8, 9
10
PS C:\PowerShell> 1..10 | Split-Collection -ChunkSize 5 | %{ $_ -join ', ' }
1, 2, 3, 4, 5
6, 7, 8, 9, 10
PS C:\PowerShell>
Then I did this, processing 50 at a time, which, as intended, seems to work around the issue:
PS C:\PowerShell> $Results = $Computers[0..300] | Split-Collection -ChunkSize 50 | %{ Test-MultiConnection $_ }
PS C:\PowerShell> $Results.Count
301
PS C:\PowerShell> $Results | select -first 3 | ft -a computername, online
ComputerName Online
------------ ------
comp1 False
comp2 True
comp3 False
Shrug. If someone can fill in the blanks on the throttling/session limits, it’d be nice.