Am trying to do a simple popup window with a count down timer. But the timer never does the countdown instead its just showing as static data as 00:00. Here is the code:
Add-Type -AssemblyName System.Windows.Forms
Create Form
$Form = New-Object System.Windows.Forms.Form
$Form.Text = “Windows Restart”
$Form.Size = New-Object System.Drawing.Size(415, 200)
$form.ClientSize = ‘415,200’
$Form.StartPosition = “CenterScreen”
$Form.FormBorderStyle = “FixedDialog”
$Form.MaximizeBox = $false
$Form.MinimizeBox = $false
Message Label
$MessageLabel = New-Object System.Windows.Forms.Label
$MessageLabel.Text = “To ensure your computer has the latest security updates thisn" + "system will restart at 2:00 a.m. Please make sure to save your
n” +
“documents and close any open applications.n
n” +
“To defer the restart, please click on Exit Without Restart.”
$MessageLabel.Location = New-Object System.Drawing.Point(25, 15)
$MessageLabel.Size = New-Object System.Drawing.Size(385, 88)
$MessageLabel.TabIndex = 1
$MessageLabel.Font = ‘Segoe UI, 10pt’
$Form.Controls.Add($MessageLabel)
Countdown Text Label (normal font)
$CountdownTextLabel = New-Object System.Windows.Forms.Label
$CountdownTextLabel.Location = New-Object System.Drawing.Point(204, 125)
$CountdownTextLabel.Size = New-Object System.Drawing.Size(115, 25)
$CountdownTextLabel.TabIndex = 2
$CountdownTextLabel.Font = ‘Segoe UI, 10pt’
$CountdownTextLabel.Text = “Time remaining:”
$Form.Controls.Add($CountdownTextLabel)
Countdown Value Label (bold font)
$CountdownLabel = New-Object System.Windows.Forms.Label
$CountdownLabel.Location = New-Object System.Drawing.Point(305, 125)
$CountdownLabel.Size = New-Object System.Drawing.Size(60, 25)
$CountdownLabel.Font = New-Object System.Drawing.Font(“Segoe UI”, 10, [System.Drawing.FontStyle]::Bold)
$CountdownLabel.Text = ‘00:00:00’
$Form.Controls.Add($CountdownLabel)
Exit Button
$ExitButton = New-Object System.Windows.Forms.Button
$ExitButton.Text = “Exit Without Restart”
$ExitButton.Location = New-Object System.Drawing.Point(140, 165)
$ExitButton.Size = New-Object System.Drawing.Size(125, 25)
$ExitButton.TabIndex = 4
$ExitButton.Font = ‘Segoe UI, 09pt’
$ExitButton.UseCompatibleTextRendering = $true
$ExitButton.Add_Click({ $Form.Close() })
$Form.Controls.Add($ExitButton)
Restart Button
$RestartButton = New-Object System.Windows.Forms.Button
$RestartButton.Text = “Restart Now”
$RestartButton.Location = New-Object System.Drawing.Point(280, 165)
$RestartButton.Size = New-Object System.Drawing.Size(125, 25)
$RestartButton.TabIndex = 5
$RestartButton.Font = ‘Segoe UI, 09pt’
$RestartButton.UseCompatibleTextRendering = $true
$RestartButton.Add_Click({ Start-Process “shutdown” -ArgumentList “/r /t 0” })
$Form.Controls.Add($RestartButton)
Timer and Countdown
$RemainingSeconds = 3600
$Timer = New-Object System.Windows.Forms.Timer
$Timer.Interval = 1000
$Timer.Add_Tick({
$RemainingSeconds--
$TimeSpan = [TimeSpan]::FromSeconds($RemainingSeconds)
# Update the label using BeginInvoke method
$Form.BeginInvoke([Action]{
$CountdownLabel.Text = $TimeSpan.ToString("hh\:mm\:ss")
})
if ($RemainingSeconds -le 0) {
$Timer.Stop()
Start-Process "notepad.exe"
}
})
Start the Timer after initializing the form
$Form.Add_Shown({ $Timer.Start() })
Show the form
$Form.ShowDialog()