I’m working on adding a new feature on the Show-InstallationPrompt to display a visible timer when using the -Timeout switch. Currently, there is a countdown timer, but there is no physical countdown display.
I’ve been succesful in getting the countdown timer to display when the -Timeout switch by reusing code from Show-InstallationRestartPrompt. It is properly indicating the correct timeout period in display, but the timer is not ticking down
This was a similar issue to what I had posted on earlier when I had a bracketing issue with AppDeployToolkitMain.ps1 when the Show-InstallationWelcome was also displaying a countdown to force exit applications, but not counting down. However, I did confirm that code was working and the visible countdown was ticking down in this instance with a brand new Deploy-Application.ps1. I also confirmed with Show-InstallationPrompt that even though the ticker was not visibly counting down, it did countdown and exhibited the behavior I needed it too.
I’m hoping this code will help someone else out that needed this feature as well as resolving the visible countdown issue. I checked my bracketing in AppDeployToolkitMain, and it shows the links properly in Notepad++, but another pair of eyes might be able to spot the issue right away and see where I might have gone wrong.
Here are the code snippets
Deploy-Application.ps1 snippet
<code>If ($deploymentType -ine 'Uninstall') {
##*===============================================
##* PRE-INSTALLATION
##*===============================================
[string]$installPhase = 'Pre-Installation'
## Show Welcome Message, close Internet Explorer if required, allow up to 3 deferrals, verify there is enough disk space to complete the install, and persist the prompt
Show-InstallationPrompt -Message "Test Content." -ButtonLeftText 'Cancel' -ButtonRightText 'OK' -Icon 'Warning' -Timeout 60 -ExitonTimeOut $false
Show-InstallationWelcome -CloseApps 'iexplore' -BlockExecution -CloseAppsCountdown 600
## Show Progress Message (with the default message)
Show-InstallationProgress
## <Perform Pre-Installation tasks here>
</code>
sorry, I keep trying to add more code, but it keeps bouncing out. Here’s what I added in shorter bites.
Process {
## Bypass if in non-interactive mode
If ($deployModeSilent) {
Write-Log -Message "Bypassing Installation Prompt [Mode: $deployMode]... $Message" -Source ${CmdletName}
Return
}
## Get parameters for calling function asynchronously
[hashtable]$installPromptParameters = $psBoundParameters
## Check if the countdown was specified
If ($timeout -gt $configInstallationUITimeout) {
[string]$CountdownTimeoutErr = "The installation UI dialog timeout cannot be longer than the timeout specified in the XML configuration file."
Write-Log -Message $CountdownTimeoutErr -Severity 3 -Source ${CmdletName}
Throw $CountdownTimeoutErr
}
[Windows.Forms.Application]::EnableVisualStyles()
$formInstallationPrompt = New-Object -TypeName 'System.Windows.Forms.Form'
$pictureBanner = New-Object -TypeName 'System.Windows.Forms.PictureBox'
$pictureIcon = New-Object -TypeName 'System.Windows.Forms.PictureBox'
$timerCountdown = New-Object -TypeName 'System.Windows.Forms.Timer'
$labelCountdown = New-Object -TypeName 'System.Windows.Forms.Label'
$labelTimeRemaining = New-Object -TypeName 'System.Windows.Forms.Label'
$labelText = New-Object -TypeName 'System.Windows.Forms.Label'
$buttonRight = New-Object -TypeName 'System.Windows.Forms.Button'
$buttonMiddle = New-Object -TypeName 'System.Windows.Forms.Button'
$buttonLeft = New-Object -TypeName 'System.Windows.Forms.Button'
$buttonAbort = New-Object -TypeName 'System.Windows.Forms.Button'
$InitialFormInstallationPromptWindowState = New-Object -TypeName 'System.Windows.Forms.FormWindowState' `
<code> ## Label Time Remaining
$labelTimeRemaining.Location = '20,138'
$labelTimeRemaining.Name = 'labelTimeRemaining'
$labelTimeRemaining.Size = $buttonsize
$labelTimeRemaining.TabIndex = 4
$labelTimeRemaining.Text = $timeout
$labelTimeRemaining.TextAlign = 'MiddleCenter'
## Label Countdown
$labelCountdown.Font = 'Microsoft Sans Serif, 18pt, style=Bold'
$labelCountdown.Location = '170,200'
$labelCountdown.Name = 'labelCountdown'
$labelCountdown.TabIndex = 5
$labelCountdown.Text = '00:00:00'
$labelCountdown.TextAlign = 'MiddleCenter'
[datetime]$startTime = Get-Date
[datetime]$countdownTime = $startTime
## Timer
[datetime]$countdownTime = $startTime.AddSeconds($timeout)
$timer = New-Object -TypeName 'System.Windows.Forms.Timer'
$timer.Interval = ($timeout * 1000)
$timer.Add_Tick({
Write-Log -Message 'Installation action not taken within a reasonable amount of time.' -Source ${CmdletName}
$buttonAbort.PerformClick()
})
$timer.Start()
[timespan]$remainingTime = $countdownTime.Subtract($Starttime)
$labelCountdown.Text = [string]::Format('{0}:{1:d2}:{2:d2}', $remainingTime.Hours, $remainingTime.Minutes, $remainingTime.Seconds)</code>