3

我有多个命令在单独的运行空间中并行运行,由RunspacePool.

如何确定每个命令正在运行的运行空间?

param (
    $MaxRunspaces = 4,
    $JobCount = 20
)
try {
    $pool = [RunspaceFactory]::CreateRunspacePool(1, $MaxRunspaces)
    $pool.Open()
    $jobs = 1..$JobCount | foreach {
        $ps = [PowerShell]::Create()
        $ps.RunspacePool = $pool
        [void]$ps.AddScript({
            # get current runspace here ???
            # $runspace = ...
        })
        [PSCustomObject]@{
            PowerShell = $ps
            AsyncResult = $ps.BeginInvoke()
        }
    }
    $jobs | foreach {
        $_.PowerShell.EndInvoke($_.AsyncResult)
        $_.PowerShell.Dispose()
    }
}
finally {
    $pool.Dispose()
}
4

0 回答 0