多个 Powershell 进度条(嵌套?)

Multiple Powershell Progress Bars (Nested?)

我正在寻找一种显示多个进度条的方法,一个用于外循环,一个用于内循环。我有一个循环遍历大量自定义对象的脚本,对于这些对象中的每一个,我都有一个内部循环来对作为这些对象属性的列表执行操作。

脚本示例:

$ListOfIDs.Keys |
Show-Progress | #Outer Loop - ProgressBar1
% {
    $entityName = $_
    $TableIndex = $ListOfEntities.Name.IndexOf($entityName)
    $TableNameList = $ListOfEntities[$TableIndex].Group.RoleTable

    $ListOfIDS[$_] |
    Show-Progress | #Inner Loop - ProgressBar2
    % {
        $ID = $_.ID
        [PSCustomObject] @{
            EntityName = $entityName
            Id = $ID
            Roles =  $TableNameList | Function-FooBar -ID $ID
        }
    }
} 

显示进度功能:

function Show-Progress
{
[CmdletBinding()]
param (
    [Parameter(Mandatory=$true, Position=0, ValueFromPipeline=$true)]
    [PSObject[]]$InputObject
)

    [int]$TotItems = $Input.Count
    [int]$Count = 0

    $Input|foreach {
        $_
        $Count++
        [int]$PercentComplete = ($Count/$TotItems* 100)
        Write-Progress -Activity "Processing items" -PercentComplete $PercentComplete -Status ("Working - " + $PercentComplete + "%")
    }
}

这是我正在寻找的一个简单示例:

您可以使用参数 -ParentId-Id 来实现。在外循环中,您指定 ID 1,在内循环中,您将值 1 指定为 ParentId

https://www.powershellgallery.com/packages/write-ProgressEx

https://github.com/mazzy-ax/Write-ProgressEx

示例:

$outer = 1..20
$inner = 1..50

write-ProgressEx "pipe nodes" -Total $outer.Count
$outer | write-ProgressEx -Status "outer" | ForEach-Object {

    write-ProgressEx "pipe names" -Total $inner.Count -id 1
    $inner | write-ProgressEx -id 1 -status "inner" | ForEach-Object {
        # ....
    }

}
write-ProgressEx #close all progress bars