多个Powershell进度条(嵌套?)

时间:2015-10-23 10:47:43

标签: powershell progress-bar

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

脚本示例:

$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 + "%")
    }
}

以下是我要查找的内容的简短示例:ProgressExample

2 个答案:

答案 0 :(得分:14)

您可以使用参数-ParentId-Id来完成此操作。在外循环中,您可以分配ID 1,在内循环中,您可以将值{1}指定为ParentId

答案 1 :(得分:5)

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

enter image description here