Last active
April 28, 2022 07:00
-
-
Save michaelvdnest/e2eff62fd1f4a8e9ddeb1933576c0dc0 to your computer and use it in GitHub Desktop.
An example of calculating ETA for a PowerShell progress bar.
This file contains 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
# Data Source | |
$items = 1..85 | |
# Loop | |
$items | ForEach-Object ` | |
-Begin { | |
# Initialize Tracking | |
$total = $items.Count | |
$start = Get-Date | |
$i = 0 | |
} ` | |
-Process { | |
# Progress Tracking | |
$i++ | |
$prct = $i / $total | |
$elapsed = (Get-Date) - $start | |
$totalTime = ($elapsed.TotalSeconds) / $prct | |
$remain = $totalTime - $elapsed.TotalSeconds | |
$eta = (Get-Date).AddSeconds($remain) | |
# Display | |
$activity = "Testing ETA" | |
$status = ("$($prct.ToString('P')) % ($i/$total) {0:dd\.hh\:mm\:ss} eta $eta" -f (New-TimeSpan -seconds $remain)) | |
Write-Progress -Activity $activity -Status $status -PercentComplete ($prct * 100) | |
# Operation | |
Start-Sleep -Milliseconds (Get-Random -Minimum 100 -Maximum 500) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment