Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple Powershell Progress Bars (Nested?)

I'm looking for a way to display multiple progress bars, one for an outer loop and one for an inner. I have a script that Loops through a big list of custom objects, and for each of those objects I have an inner loop that performs actions on a list that is an attribute of those objects.

Script Example:

$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
        }
    }
} 

Show-Progress Function:

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

Here is a quick example of what I am looking for : ProgressExample

like image 771
K.Burrell Avatar asked Oct 23 '15 10:10

K.Burrell


2 Answers

You can use the parameter -ParentId and -Id to accomplish that. At the Outer Loop you assign the ID 1, and at the Inner Loop you specify as ParentId the value 1.

like image 65
Johan de Haan Avatar answered Nov 02 '22 23:11

Johan de Haan


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

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

sample:

$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

like image 5
mazzy Avatar answered Nov 02 '22 21:11

mazzy