Common/Public/Remove-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
function Remove-RunspacePool
{
    [CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'Low')]
    param
    (
        [Parameter(ValueFromPipeline)]
        [System.Management.Automation.Runspaces.RunspacePool[]]
        $RunspacePool
    )

    process
    {
        foreach ($pool in $RunspacePool)
        {
            if ($PSCmdlet.ShouldProcess($pool.InstanceId, 'Closing runspace pool'))
            {
                $max = $pool.GetMaxRunspaces()
                $state = $pool.ApartmentState

                $pool.Close()
                $pool.Dispose()

                Remove-Variable -Name "ALCommonRunspacePool_$($max)_$($state)" -Scope Global -ErrorAction SilentlyContinue
            }
        }
    }
}