PoshCode Logo PowerShell Code Repository

Start-Timer (modification of post by view diff)
embed code: <script type="text/javascript" src="http://PoshCode.org/embed/161"></script>download | new post

The kitchen timer with a kitchen sink -full of features

  1. ## Start-Timer.ps1
  2. ## A kitchen timer script with visible countdown and customizable audio alert and messages
  3. ####################################################################################################
  4. param( $seconds=0,  $reason="The Timer",  $SoundFile="$env:SystemRoot\Media\notify.wav",
  5.       $minutes=0,  $hours=0,  $timeout=0,  [switch]$novoice, [string[]]$status="Cooking","Burning"
  6. )
  7.  
  8. $start = [DateTime]::Now
  9. write-progress -activity $reason -status "Running"
  10.  
  11. $seconds = [Math]::Abs($seconds) + [Math]::Abs($minutes*60) + [Math]::Abs($hours*60*60)
  12. $end = $start.AddSeconds( $seconds )
  13.  
  14. ## Take care of as much overhead as we can BEFORE we start counting
  15. ## So the sounds can play as cloase to the end as possible
  16. $Voice = new-object -com SAPI.SpVoice
  17. $Sound = new-Object System.Media.SoundPlayer
  18.  
  19. if(Test-Path $soundFile) {
  20.    $Sound.SoundLocation=$SoundFile
  21. } else {
  22.    $soundFile = $Null
  23. }
  24.  
  25. ## The actual timing loop ...
  26. do {
  27.    $remain  = ($end - [DateTime]::Now).TotalSeconds
  28.    $percent = [Math]::Min(100, ((($seconds - $remain)/$seconds) * 100))
  29.    $sec     = [Math]::Max(000, $remain)
  30.  
  31.    write-progress -activity $reason -status $status[0] -percent $percent -seconds $sec
  32.    start-sleep -milli 100
  33. } while( $remain -gt 0 )
  34.  
  35. ## And a loop for beeping
  36. $Host.UI.RawUI.FlushInputBuffer()
  37. do {
  38.    if($SoundFile) {
  39.       $Sound.PlaySync()
  40.    } else {
  41.       [System.Media.SystemSounds]::Exclamation.Play()
  42.    }
  43.    if(!$novoice -and !$Host.UI.RawUI.KeyAvailable){
  44.      $null = $Voice.Speak( "$reason is $($status[1])!!", 0 )
  45.    }
  46.  
  47.    write-progress -activity $reason -status $status[1] -percent ([Math]::Min(100,((($seconds - $remain)/$seconds) * 100))) -seconds ([Math]::Max(0,$remain))
  48.    $remain = ($end - [DateTime]::Now).TotalSeconds
  49. } while( !$Host.UI.RawUI.KeyAvailable -and ($timeout -eq 0 -or $remain -gt -$timeout))
  50.  
  51. if($SoundFile) {
  52.    $Sound.Stop()
  53. }

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me