-
-
Save n-engelhardt/ca6157c0a8409ac94a42cdbafebb8278 to your computer and use it in GitHub Desktop.
Progress bar for current "Transferring" BITS Transfers with a time remain calculation.
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
| param ( | |
| [guid]$jobID | |
| ) | |
| #Dynamically choose whether to return one or all based on if a Job ID was provided | |
| function DynamicBitsStatus { | |
| If ($jobID) { | |
| return (Get-BitsTransfer -JobId $jobID | Where-Object { $_.JobState -eq "Transferring" }) | |
| } Else { | |
| return (Get-BitsTransfer | Where-Object { $_.JobState -eq "Transferring" }) | |
| } | |
| } | |
| #Borrowed from https://theposhwolf.com/howtos/Format-Bytes/ | |
| Function Format-Bytes { | |
| Param | |
| ( | |
| [Parameter( | |
| ValueFromPipeline = $true | |
| )] | |
| [ValidateNotNullOrEmpty()] | |
| [float]$number | |
| ) | |
| Begin{ | |
| $sizes = 'KB','MB','GB','TB','PB' | |
| } | |
| Process { | |
| # New for loop | |
| for($x = 0;$x -lt $sizes.count; $x++){ | |
| if ($number -lt [int64]"1$($sizes[$x])"){ | |
| if ($x -eq 0){ | |
| return "$number B" | |
| } else { | |
| $num = $number / [int64]"1$($sizes[$x-1])" | |
| $num = "{0:N2}" -f $num | |
| return "$num $($sizes[$x-1])" | |
| } | |
| } | |
| } | |
| } | |
| End{} | |
| } | |
| while (DynamicBitsStatus) { | |
| $totalbytes=0; | |
| $bytestransferred=0; | |
| $timeTaken = 0; | |
| foreach ($job in (DynamicBitsStatus | Where-Object { $_.JobState -eq "Transferring" } | Sort-Object CreationTime)) { | |
| $totalbytes += $job.BytesTotal; | |
| $bytestransferred += $job.bytestransferred | |
| if ($timeTaken -eq 0) { | |
| #Get the time of the oldest transfer aka the one that started first | |
| $timeTaken = ((Get-Date) - $job.CreationTime).TotalMinutes | |
| } | |
| } | |
| #TimeRemaining = (TotalFileSize - BytesDownloaded) * TimeElapsed/BytesDownloaded | |
| if ($totalbytes -gt 0) { | |
| $timeLeft = ($totalBytes - $bytestransferred) * ($timeTaken / $bytestransferred) | |
| $pctComplete = $(($bytestransferred*100)/$totalbytes); | |
| $timeUnit = "minute(s)" | |
| If ($timeLeft -gt 60) { | |
| $timeLeft = ($timeLeft / 60) | |
| $timeUnit = "hour(s)" | |
| } | |
| If (($timeUnit -eq "hour(s)") -and ($timeLeft -gt 24)) { | |
| $timeLeft = ($timeLeft / 24) | |
| $timeUnit = "day(s)" | |
| } | |
| $formattedBytesTransferred = (Format-Bytes($bytestransferred)) | |
| $formattedBytesTotal = (Format-Bytes($totalbytes)) | |
| $formattedPctComplete = ("{0:p}" -f ($pctComplete/100)) | |
| $roundedTimeRemaining = [math]::Round($timeLeft,2) | |
| Write-Progress -Status "Transferring $formattedBytesTransferred of $formattedBytesTotal ($formattedPctComplete). $roundedTimeRemaining $timeUnit remaining." -Activity "Dowloading files" -PercentComplete $pctComplete | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment