Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save pashalvov/76488696c14718acdda2dc359580ccd3 to your computer and use it in GitHub Desktop.
Save pashalvov/76488696c14718acdda2dc359580ccd3 to your computer and use it in GitHub Desktop.
An example of calculating ETA for a PowerShell progress bar.
# 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)
}
@pashalvov
Copy link
Author

Прикольный расчёт времени прогресса

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment