-
-
Save developerprofiles/606b922269fe6a3c7ed3b7019dd977e8 to your computer and use it in GitHub Desktop.
A PowerShell function to display an event countdown.
This file contains hidden or 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 hidden or 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
Hi. I sent a message as issue to you a few months ago (to developerprofile account, I guess it yours). I have a question about a repository that you have forked. I am going to delete my github account (not this account) but you (and other users) have forked my repository. If I ask, would you delete the repository? If you reply this comment can write what repository that would you delete. Looking forward to your reply because it's important for me. Also I have contacted with Github Support too and they said me that I should direct contact with you. Thx.