Start-SleepProgress: Pause script, show countdown timer, progress bar, message, and nicely formatted time strings

Topics: Archive - Toolkit Extensions
Developer
Jul 10, 2014 at 6:05 AM
Function Start-SleepProgress
{
<#
    .SYNOPSIS
        Pause script execution for specified time and show a countdown timer with progress bar and message.
        
    .DESCRIPTION
        Sleep for a specified amout of time. Show a progress bar with a countdown timer and message.
        The countdown timer formats the time so that it is easy to tell time remaining
        using the largest time values that makes sense for the amount of time remaining.
        
    .PARAMETER SleepHash
        The length of time that script should be paused passed as a hash value.
        Time can be specified in any combination of seconds, minutes, or hours.
        Examples:
            $SleepTime = @{"Seconds"=10}
            $SleepTime = @{"Minutes"=10}
            $SleepTime = @{"Hours"=10}
            $SleepTime = @{"Hours"=1; "Minutes"=10; "Seconds"=30}
        
    .PARAMETER Message
        The message you wish to display on the progress message.
        Default message is: Please wait for script execution to continue...
        
    .EXAMPLE
        $SleepTime = @{"Minutes" = 10}
        Start-SleepProgress $SleepTime
        
        $SleepTime = @{"Hours"=1; "Minutes"=10; "Seconds"=30}
        Start-SleepProgress $SleepTime
        
    .NOTES
#>
    
    [CmdletBinding()]
    Param
    (
        [parameter(Mandatory=$true)]
        [hashtable]$SleepHash,
        
        [parameter(Mandatory=$false)]
        [string]$Message = "Please wait for script execution to continue..."
    )
    
    Function Get-FormattedTimeString ($Seconds)
    {
        $TimeSpan = New-TimeSpan -Seconds $Seconds
        If (($TimeSpan.Hours -eq 0) -and ($TimeSpan.Minutes -ge 1))
        {
            $TimeSpanString = '{0:00}m:{1:00}s' -f $TimeSpan.Minutes,$TimeSpan.Seconds
        }
        ElseIf (($TimeSpan.Hours -eq 0) -and ($TimeSpan.Minutes -eq 0))
        {
            $TimeSpanString = '{0:00}s' -f $TimeSpan.Seconds
        }
        ElseIf ($TimeSpan.Hours -ge 1)
        {
            $TimeSpanString = '{0:00}h:{1:00}m:{2:00}s' -f $TimeSpan.Hours,$TimeSpan.Minutes,$TimeSpan.Seconds
        }
        Return $TimeSpanString
    }
    
    [int]$SleepSeconds = 0
    
    ForEach ($Key in $SleepHash.Keys)
    {
        Switch($Key)
        {
            "Seconds" {
                        $SleepSeconds = $SleepSeconds + $SleepHash.Get_Item($Key)
                      }
            "Minutes" {
                        $SleepSeconds = $SleepSeconds + ($SleepHash.Get_Item($Key) * 60)
                      }
            "Hours"   {
                        $SleepSeconds = $SleepSeconds + ($SleepHash.Get_Item($Key) * 60 * 60)
                      }
        }
    }
    
    $TotalTimeSpanString = Get-FormattedTimeString -Seconds $SleepSeconds
    For ($Count=0; $Count -lt $SleepSeconds; $Count++)
    {
        $CountDownString = Get-FormattedTimeString -Seconds ($SleepSeconds - $Count)
        Write-Progress -Activity $Message -Status "Waiting for $TotalTimeSpanString, $CountDownString remaining" -PercentComplete ($Count/$SleepSeconds*100)
        Start-Sleep -Seconds 1
    }
    Write-Progress -Activity "Waiting for $TotalTimeSpanString, $CountDownString remaining" -Completed
}
Feb 5, 2015 at 10:15 PM
The progress bar and countdown do not show up on the screen. Show-InstallationWelcome dialogs show up before and after. It seems to process, but does not show anything. Is Write-Progress only for inside of a powershell command window?
Developer
Mar 12, 2015 at 1:59 AM
Yes, it is only for inside a powershell command window. This is more for sys admin type script people write.