Final yr I launched a PowerShell module referred to as PSClock. The module incorporates a command to create a clear WPF kind displaying a clock. Shortly after, somebody posted a request for a countdown timer. Not an unreasonable request and one I lastly obtained round to implementing. Nevertheless, I already had a module with numerous timer and countdown-related instructions referred to as PSTimers I had been considering of a WPF-based timer, so this solved two issues.
I’ve printed v1.0.1 of the PSTimers module to the PowerShell Gallery with a brand new command, Begin-PSCountdownTimer. This command relies on Begin-PSClock and can launch a WPF-based countdown timer. The timer’s look and conduct is customizable through parameters.
Begin-PSCountdownTimer -Seconds 300 -FontSize 48 -Coloration Inexperienced -Message "The PowerShell magic begins in "
At 50 seconds, the font shade will change to Yellow, and at 30 seconds, to Pink. These settings will also be set with parameters.
The timer makes use of a synchronized hashtable.
PS C:> $pscountDownClock
Title Worth
---- -----
FontStyle Regular
Warning 30
StartingPosition
AlertColor Yellow
WarningColor Pink
FontWeight Regular
Message The PowerShell magic begins in
OnTop False
Seconds 300
FontSize 48
CurrentPosition {276, 964}
Operating True
Runspace System.Administration.Automation.Runspaces.LocalRunspace
Alert 50
Coloration Inexperienced
Began 10/19/2022 12:35:00 PM
FontFamily Segoi UI
After the clock is operating, you possibly can modify the hashtable to regulate the WPF kind.
PS C:> $PScountdownclock.fontsize = 60
PS C:> $PScountdownclock.shade = "chartreuse"
If you wish to cease the clock early you possibly can run Cease-PSCountdownTimer or set $PSCountdownClock.Operating
to $False.
The countdown timer runs in a separate runspace, so your immediate isn’t blocked. However this additionally signifies that it will possibly’t simply work together with the PowerShell session that launched it. If you want one thing to occur on the finish of the timer, I recommend writing an automation script across the command.
<#
A proof-of-concept script that provides an motion on the finish of the PSCountdownTimer
#>
Begin-PSCountdownTimer -seconds 60 -message "The PowerShell magic begins in " -FontSize 64 -Coloration SpringGreen
Do {
Begin-Sleep -Seconds 1
} Whereas ($PScountdownclock.Operating)
Clear-Host
Write-Host "Are you prepared for some PowerShell?" -ForegroundColor magenta -BackgroundColor grey
Add-Kind -AssemblyName PresentationCore
$filename = "c:work 1-Begin.mp3"
#the media participant launches with no UI. Use the article's strategies to regulate it.
$international:mediaplayer = New-Object system.home windows.media.mediaplayer
$international:mediaPlayer.Open($filename)
$international:mediaplayer.Play()
# mediaplayer.cease()
# $mediaplayer.shut()
I believe I’ll begin utilizing this in my lessons and convention displays.
The module ought to work cross-platform, however the WPF-based instructions require Home windows. In the event you strive it out, I’d like to know what you assume.