Common/Public/Get-RunspacePool.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
function Get-RunspacePool
{
    [OutputType([System.Management.Automation.Runspaces.RunspacePool[]])]
    [CmdletBinding()]
    param
    (
        [Parameter()]
        [int]
        $ThrottleLimit,

        [Parameter()]
        [System.Threading.ApartmentState]
        $ApartmentState
    )

    $pools = $(Get-Variable -Name ALCommonRunspacePool_* -ErrorAction SilentlyContinue).Value

    if ($ThrottleLimit)
    {
        $pools = $pools.Where({$_.GetMaxRunspaces() -eq $ThrottleLimit})
    }

    if ($ApartmentState)
    {
        $pools = $pools.Where({$_.ApartmentState -eq $ApartmentState})
    }

    $pools
}