2

I have a PowerShell function that calls Write-Progress. In another function, I would like to get the state of the displayed progress. Is it possible to query the state of the displayed progress?

The use case is this:

  • I have function A that calls function B 10 times.
  • Each time function A calls function B, Write-Progress is called to update the -PercentComplete.
  • Within function B, I would like to update the -PercentComplete of the progress, but I don't know what the current percent complete is. I also don't want to pass around a "Progress" object to B if I can query the displayed progress object.

I have tagged this as because that is what my environment is.

I have tried looking in the $host variable, as well as the $host.UI and $host.UI.RawUI and could not find what I want.

So for anyone else that is interested, I ended up defining these two functions in a module (kudos for HAL9256 for the inspiration):

function Get-Progress {
    [cmdletbinding()]
    param()

    if (-not $global:Progress) {
        $global:Progress = New-Object PSObject -Property @{
            'Activity' = $null
            'Status' = $null
            'Id' = $null
            'Completed' = $null
            'CurrentOperation' = $null
            'ParentID' = $null
            'PercentComplete' = $null
            'SecondsRemaining' = $null
            'SourceId' = $null
        }
    }

    $global:Progress
}




function Show-Progress {
    [cmdletbinding()]
    param()

    $progress = $global:Progress

    $properties = $progress.PSObject.Properties | Where {$_.MemberType -eq 'NoteProperty'}

    $parameters = @{}
    foreach ($property in $properties) {
        if ($property.Value) {
            $parameters[$property.Name] = $property.Value
        }
    }

    if ($parameters.Count) {
        Write-Progress @parameters
    }
}
1
  • ooooohhhh cool. Creating a new custom PSObject to hold the progress... nice! Commented Aug 19, 2015 at 17:59

2 Answers 2

1

There's nothing to query. You must keep track of/calculate the percentage yourself and pass it to the cmdlet, otherwise Write-Progress wouldn't know what to display.

Give function B an additional parameter and add a counter to function A:

function A {
  $i = 1
  1..10 | % {
    B (10 * $i)
    $i++
  }
}

function B($p) {
  Write-Progress -Activity 'foo' -PercentComplete $p
}
Sign up to request clarification or add additional context in comments.

1 Comment

thanks Ansgar, I understand about computing the percentage. I was hoping there was a Model-View with the progress indicator, so I could query it
1

I have experienced a similar issue, where I have a module that runs the script operations and a separate Logging module that has to log progress. The easiest, and from all the possible methods is most reliable method (and I know people will shudder) is to use a global variable.

If you don't want to have a bunch of extra parameters passed back and forth, this is the best method.

#Set global variable
$global:Progress = 10

#------ Other function -----------

#Write Progress
Write-Progress -Activity 'foo' -PercentComplete $global:Progress

1 Comment

Thanks HAL9256, looks like this is what I'll do as well

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.