Last active
December 16, 2019 09:00
-
-
Save WimObiwan/3371953841219dd4a0c7084ec1929a59 to your computer and use it in GitHub Desktop.
Get-WithProgress.ps1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Get-WithProgress { | |
param( | |
[string] $Activity = 'Processing', | |
[int] $Loop = 0 | |
) | |
# v1.0 Initial version | |
# v1.1 Only increase progress AFTER processing an item | |
# Collect the pipeline input ($Input) up front in an array, | |
# using @(...) to force enumeration. | |
$a = @($Input) | |
# Count the number of input objects. | |
$count = $a.Count | |
if ($Loop -le 0) { | |
if ($count -gt 0) { | |
$Loop = [Math]::Pow(10, [Math]::Max(0, [Math]::Floor([Math]::Log10($count) - 1.5))) | |
} else { | |
$Loop = 1 | |
} | |
} | |
$i = 0 | |
# Process the input objects one by one, with progress display. | |
$a | ForEach-Object { | |
if ($i % $Loop -eq 0) { | |
$prc = $i * 100 / $count | |
Write-Progress -PercentComplete $prc -Activity $Activity -Status "$i/$count processed" | |
} | |
$_ | |
++$i | |
} | |
Write-Progress -Completed -Activity $Activity | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment