Powershell Start-ThreadJob does not respect ThrottleLimit parameter

70 views Asked by At

Here is the code:

function sh {
    [CmdletBinding()]
    param (
        [Parameter(Position = 0, Mandatory = $true)]
        [Object[]]$Lab
    )    
    if ($Lab.Count -ne 0) {                
        
        $Lab | ForEach-Object {
            $null = Start-ThreadJob -ScriptBlock {
                param($ComputerName)
                Stop-Computer -Force -ComputerName $ComputerName
            } -ThrottleLimit 250 -ArgumentList $_
        }
    }
}

The script just shuts down a lot of workstations.. and there are less then 250 computers, but if never able to spawn threads as needed and I see computers shut down in batches of ~30-40.. Why so? I do specify ThrottleLimit

I also tried:

ForEach-Object -UseNewRunspace -Parallel {} -AsJob

ForEach-Object -UseNewRunspace -Parallel {
        Invoke-Command -Computername ...etc
} -AsJob

None of the methods provide simultaneous true parallel execution at scale!
The thing I didn't tried: Working with Runspaces - Shutdown every machine in new Runspace may it work?

0

There are 0 answers