-
-
Save Chirishman/35ccdd61cb2bec84eb1b75c150372dac to your computer and use it in GitHub Desktop.
Powershell sleep function, with 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
function Start-ProgressSleep { | |
Param( | |
[Parameter(ParameterSetName='seconds',Mandatory=$true)] | |
[int]$seconds, | |
[Parameter(ParameterSetName='span',ValueFromPipeline,Mandatory=$true)] | |
[timespan]$timespan | |
) | |
<# | |
.SYNOPSIS | |
Function to Start-Sleep with a progress bar | |
.DESCRIPTION | |
Duplicates the functionality of the 'Start-Sleep' command but adds a progress bar. Time is passed to the function in seconds as an argument or as a timespan. | |
.NOTES | |
# Updated from original to include the 'Wait time' in minutes and seconds | |
.EXAMPLE | |
Start-ProgressSleep -seconds 300 | |
.EXAMPLE | |
Start-ProgressSleep -timespan 1:30:15 | |
.LINK | |
https://gist.github.com/evoelker/fcd8dc1563e15a6f8e5e11fdd93880cf | |
https://gist.github.com/ctigeek/bd637eeaeeb71c5b17f4 | |
#> | |
$WaitSeconds = $( | |
switch ($PsCmdlet.ParameterSetName){ | |
'seconds' { | |
$seconds | |
} | |
'span' { | |
$timespan.TotalSeconds | |
} | |
} | |
) | |
$doneDT = (Get-Date).AddSeconds($WaitSeconds) | |
$TotalWaitTime = "Waiting $($([timespan]::fromseconds($WaitSeconds)).ToString("hh\:mm\:ss"))..." | |
while($doneDT -gt (Get-Date)) { | |
$secondsLeft = $doneDT.Subtract((Get-Date)).TotalSeconds | |
$percent = ($WaitSeconds - $secondsLeft) / $WaitSeconds * 100 | |
Write-Progress -Activity $TotalWaitTime -Status "Waiting..." -SecondsRemaining $secondsLeft -PercentComplete $percent | |
[System.Threading.Thread]::Sleep(500) | |
} | |
Write-Progress -Activity $TotalWaitTime -Status "Done" -SecondsRemaining 0 -Completed | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment