Powershell运行空间迭代

Powershell运行空间迭代,powershell,Powershell,我已经编写了一个脚本,可以量化图像,将所有内容复制到不同的文件夹中,并根据特定的设备进行缩放。它现在在运行空间中工作,速度更快,但是无论我深入到powershell实例中有多深,我都无法根据脚本在其中一个线程中运行的次数进行迭代 $MaxThreads = 4 #Create Run Spaces $RunspacePool = [runspacefactory]::CreateRunspacePool(1,$MaxThreads) $RunspacePool.Open() $ScriptB

我已经编写了一个脚本,可以量化图像,将所有内容复制到不同的文件夹中,并根据特定的设备进行缩放。它现在在运行空间中工作,速度更快,但是无论我深入到powershell实例中有多深,我都无法根据脚本在其中一个线程中运行的次数进行迭代

$MaxThreads = 4

#Create Run Spaces
$RunspacePool = [runspacefactory]::CreateRunspacePool(1,$MaxThreads)
$RunspacePool.Open()

$ScriptBlock = {
    Param (
        [array]$files
    )

    #copy files
    #quantize images

}

$RunningJobs = New-Object System.Collections.ArrayList
1..$MaxThreads | ForEach-Object {

    $src          =   $gfxfolder

    $Job = [powershell]::Create()
    $Job.RunspacePool = $RunspacePool

    [void]$Job.AddScript($ScriptBlock)
    [void]$Job.AddArgument($files)

    [void]$RunningJobs.Add((
        [pscustomobject]@{
            Files = $files
            PowerShell = $Job
            Handle = $Job.BeginInvoke()
        }
    ))
}

While ($RunningJobs.handle[0].IsCompleted -eq $false) {
    #do stuff whilst it's running, handle[0] always finishes last and is a good approximation of the progress
}

$return = $RunningJobs | ForEach-Object {
    $_.powershell.EndInvoke($_.handle)
    $_.PowerShell.Dispose()
}

$RunningJobs.clear()
$RunspacePool.Close()
在这种情况下,我希望每次运行$ScriptBlock时都进行计数,以便将详细信息传递给进度条


如果这是不可能的,或者有一个更简单的方法来做到这一点,我希望知道如何。谢谢

计算输出?下面是powershell 7中的foreach对象-并行。嗯,写进度更新得不太正确

1..20 | foreach -parallel { sleep 5; "$_ done" } | 
  foreach { $i = 1 } { 
  $_
  write-progress progress ($i/20*100) -percent ($i++/20*100) }

计算产量?下面是powershell 7中的foreach对象-并行。嗯,写进度更新得不太正确

1..20 | foreach -parallel { sleep 5; "$_ done" } | 
  foreach { $i = 1 } { 
  $_
  write-progress progress ($i/20*100) -percent ($i++/20*100) }