Last active
November 10, 2023 15:41
-
-
Save jdhitsolutions/17bc013f3ae43c88fd7b14c0f840ad4c to your computer and use it in GitHub Desktop.
A PowerShell function to display an event countdown.
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
#requires -version 5.1 | |
Function Get-EventCountdown { | |
[cmdletbinding()] | |
[OutputType("PSCountdown")] | |
Param( | |
[Parameter(Position=0,Mandatory,ValueFromPipeline,ValueFromPipelineByPropertyName,HelpMessage = "Enter the event date and time.")] | |
[ValidateNotNullOrEmpty()] | |
[ValidateScript({ | |
if ($_ -ge (Get-Date)) { | |
$True | |
} | |
else { | |
Write-Warning "The specified events date has already occurred." | |
Throw "You must specify a future datetime." | |
$false | |
} | |
})] | |
[alias("Date")] | |
[datetime]$EventDate, | |
[Parameter(Mandatory,ValueFromPipelineByPropertyName)] | |
[ValidateNotNullOrEmpty()] | |
[alias("Name")] | |
[string]$EventName | |
) | |
Begin { | |
Write-Verbose "[$((Get-Date).TimeofDay) BEGIN ] Starting $($myinvocation.mycommand)" | |
} #begin | |
Process { | |
Write-Verbose "[$((Get-Date).TimeofDay) PROCESS] Getting event countdown for $eventName [$EventDate]" | |
$ts = New-Timespan -Start (Get-Date) -End $EventDate | |
[PSCustomObject]@{ | |
PSTypename = "PSCountdown" | |
EventName = $EventName.Trim() | |
EventDate = $EventDate | |
Countdown = $ts | |
} | |
} #process | |
End { | |
Write-Verbose "[$((Get-Date).TimeofDay) END ] Ending $($myinvocation.mycommand)" | |
} #end | |
} #close Get-EventCountdown | |
# Set variables to control formatting based on the number of days | |
# before an event is scheduled. These values are using the format.ps1xml file | |
#events almost here | |
$PSCountdownCritical = 7 | |
#events getting closer | |
$PSCountdownPending = 14 | |
Update-Formatdata $PSScriptRoot\pscountdown.format.ps1xml |
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
<?xml version="1.0" encoding="UTF-8"?> | |
<!-- | |
Format type data generated 04/30/2021 09:33:14 by PROSPERO\Jeff | |
This file was created using the New-PSFormatXML command that is part | |
of the PSScriptTools module. | |
https://github.com/jdhitsolutions/PSScriptTools | |
--> | |
<Configuration> | |
<ViewDefinitions> | |
<View> | |
<!--Created 04/30/2021 09:33:14 by PROSPERO\Jeff--> | |
<Name>default</Name> | |
<ViewSelectedBy> | |
<TypeName>PSCountdown</TypeName> | |
</ViewSelectedBy> | |
<TableControl> | |
<!--Delete the AutoSize node if you want to use the defined widths. | |
<AutoSize />--> | |
<TableHeaders> | |
<TableColumnHeader> | |
<Label>EventName</Label> | |
<Width>35</Width> | |
<Alignment>left</Alignment> | |
</TableColumnHeader> | |
<TableColumnHeader> | |
<Label>EventDate</Label> | |
<Width>30</Width> | |
<Alignment>left</Alignment> | |
</TableColumnHeader> | |
<TableColumnHeader> | |
<Label>Countdown</Label> | |
<Width>13</Width> | |
<Alignment>right</Alignment> | |
</TableColumnHeader> | |
</TableHeaders> | |
<TableRowEntries> | |
<TableRowEntry> | |
<TableColumnItems> | |
<TableColumnItem> | |
<ScriptBlock> | |
If ($host.name -match "console") { | |
if ($_.countdown.totaldays -le $PSCountdownCritical) { | |
"$([char]27)[91m$($_.EventName)$([char]27)[0m" | |
} | |
elseif ($_.countdown.totaldays -lt $PSCountdownPending) { | |
"$([char]27)[93m$($_.EventName)$([char]27)[0m" | |
} | |
else { | |
$_.EventName | |
} | |
} | |
else { | |
$_.EventName | |
} | |
</ScriptBlock> | |
</TableColumnItem> | |
<TableColumnItem> | |
<ScriptBlock> | |
If ($host.name -match "console") { | |
if ($_.countdown.totaldays -le $PSCountdownCritical) { | |
"$([char]27)[91m$($_.EventDate)$([char]27)[0m" | |
} | |
elseif ($_.countdown.totaldays -lt $PSCountdownPending) { | |
"$([char]27)[93m$($_.EventDate)$([char]27)[0m" | |
} | |
else { | |
$_.EventDate | |
} | |
} | |
else { | |
$_.EventDate | |
} | |
</ScriptBlock> | |
</TableColumnItem> | |
<TableColumnItem> | |
<ScriptBlock> | |
$cd = $_.Countdown.ToString("dd\.hh\:mm\:ss") | |
If ($host.name -match "console") { | |
if ($_.countdown.totaldays -le $PSCountdownCritical) { | |
"$([char]27)[91m$($cd)$([char]27)[0m" | |
} | |
elseif ($_.countdown.totaldays -lt $PSCountdownPending) { | |
"$([char]27)[93m$($cd)$([char]27)[0m" | |
} | |
else { | |
$cd | |
} | |
} | |
else { | |
$cd | |
} | |
</ScriptBlock> | |
</TableColumnItem> | |
</TableColumnItems> | |
</TableRowEntry> | |
</TableRowEntries> | |
</TableControl> | |
</View> | |
</ViewDefinitions> | |
</Configuration> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
These files are described at https://jdhitsolutions.com/blog/powershell/8357/friday-fun-counting-down-events-with-powershell/