PowerShell : minuteur (countdown/timer) pour le fun

Hello,

Une petite fonction faisant office de minuteur, juste pour le fun (quoique ça pourrait être utile aux cuisiniers-geeks) 🙂

Il y a même le son à la fin 😀

function Start-Timer
{
    param (
        [Parameter(Mandatory=$true,Position=1)]
        [ValidateSet(“minutes”,”seconds”)] 
        [string]$type,
        [Parameter(Mandatory=$true,Position=2)]
        [int]$value,
        [string]$message
    )

    if (!$message)
    {
        $message = "Timer started and set to $value $type"
    }

    switch ($type)
    {
        seconds {
        
            $minutes = [math]::Round(($value/60),2)

            foreach ($Count in (1..$value))
            {
        
                [int32]$remaining = $value - $Count
                $remainingMinutes = [math]::Round(($remaining/60),2)
                Write-Progress -Id 1 -Activity $message -Status "Waiting for $value seconds ($minutes minutes), $remaining seconds remaining ($remainingMinutes minutes)" -PercentComplete (($Count / $value) * 100)
                sleep -Seconds 1
 
                if ($remaining -lt 5)
                {
                    [console]::beep(500,100)
                } 
            }        
        }

        minutes {
               
            $seconds = $value * 60
            foreach ($Count in (1..$seconds))
                {
        
                    [int32]$remaining = $seconds - $Count
                    $remainingMinutes = [math]::Round(($remaining/60),2)
                    Write-Progress -Id 1 -Activity $message -Status "Waiting for $seconds seconds ($value minutes), $remaining seconds remaining ($remainingMinutes minutes)" -PercentComplete (($Count / $seconds) * 100)
                    sleep -Seconds 1
 
                    if ($remaining -lt 5)
                    {
                        [console]::beep(500,100)
                    } 
                }           
        }

        default {"Please choose a type of counter : minutes or seconds"}
    }  
 
    Write-Progress -Id 1 -Activity $Message -Status "Completed" -PercentComplete 100 -Completed
    [console]::beep(500,1250)
}

Pour l’essayer :

Start-Timer -type seconds -value 120 -message "Test my awsome PowerShell timer"

En image :

mytimer

Laisser un commentaire