10

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

2 Answers 2

26

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.

Sign up to request clarification or add additional context in comments.

1 Comment

This achieves what I wanted perfectly, I cant believe I never noticed the parent property, feel stupid now. Thanks for the help!
5

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.