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-Progressis called to update the-PercentComplete. - Within function B, I would like to update the
-PercentCompleteof 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 powershell-v2.0 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
}
}
PSObjectto hold the progress... nice!