Visible Timer feature added for Show-InstallationPrompt (troubleshooting)

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 &#039;Uninstall&#039;) {
		##*===============================================
		##* PRE-INSTALLATION
		##*===============================================
		[string]$installPhase = &#039;Pre-Installation&#039;
		
		## 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 &quot;Test Content.&quot; -ButtonLeftText &#039;Cancel&#039; -ButtonRightText &#039;OK&#039; -Icon &#039;Warning&#039;  -Timeout 60 -ExitonTimeOut $false
		Show-InstallationWelcome -CloseApps &#039;iexplore&#039; -BlockExecution -CloseAppsCountdown 600
		## Show Progress Message (with the default message)
		Show-InstallationProgress
		
		## &lt;Perform Pre-Installation tasks here&gt;
</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 = &#039;20,138&#039;
		$labelTimeRemaining.Name = &#039;labelTimeRemaining&#039;
		$labelTimeRemaining.Size = $buttonsize
		$labelTimeRemaining.TabIndex = 4
		$labelTimeRemaining.Text = $timeout
		$labelTimeRemaining.TextAlign = &#039;MiddleCenter&#039;
		
		## Label Countdown
		$labelCountdown.Font = &#039;Microsoft Sans Serif, 18pt, style=Bold&#039;
		$labelCountdown.Location = &#039;170,200&#039;
		$labelCountdown.Name = &#039;labelCountdown&#039;
		$labelCountdown.TabIndex = 5
		$labelCountdown.Text = &#039;00:00:00&#039;
		$labelCountdown.TextAlign = &#039;MiddleCenter&#039;
		
	[datetime]$startTime = Get-Date
   [datetime]$countdownTime = $startTime

	## Timer
			[datetime]$countdownTime = $startTime.AddSeconds($timeout)
			$timer = New-Object -TypeName &#039;System.Windows.Forms.Timer&#039;
		$timer.Interval = ($timeout * 1000)
		$timer.Add_Tick({
			Write-Log -Message &#039;Installation action not taken within a reasonable amount of time.&#039; -Source ${CmdletName}
			$buttonAbort.PerformClick()

		})
			$timer.Start()
			[timespan]$remainingTime = $countdownTime.Subtract($Starttime)
			$labelCountdown.Text = [string]::Format(&#039;{0}:{1:d2}:{2:d2}&#039;, $remainingTime.Hours, $remainingTime.Minutes, $remainingTime.Seconds)</code>

Buehler?