Need help in creating PowerShell form for customized reboot notification

Ashwini Marudi 1 Reputation point
2020-11-10T20:04:50.85+00:00

I am in mid of creating a powershell GUI for system reboot, which has dropdown(Reboot now, in 30 minutes)
Based on selection from the dropdown, it has to display the countdown timer in the GUI itself.

I have used System.Windows.Forms.Timer to show the timer, but the countdown is not updating in the GUI, please help.
initially i have set $label.Text = '00:00:00'

and below is the code used for timer:

$1second=[timespan]'0:0:0:1'
$timer1_Tick={
$script:ts1=$ts1.Subtract($1second)
Write-Host $ts1
$label.Text = $ts1.ToString('hh:mm:ss')
if($ts1.Ticks -le 0){
$timer1.Stop()

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,504 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Ian Xue (Shanghai Wicresoft Co., Ltd.) 36,086 Reputation points Microsoft Vendor
    2020-11-17T02:52:01.763+00:00

    Hi,

    Please see if this works for you

    $1second=[timespan]'0:0:0:1'  
    $ts1=[timespan]'0:0:30:0'  
    $label.Text = $ts1.ToString('hh\:mm\:ss')  
    $timer1 = New-Object system.Windows.Forms.timer  
    $timer1.Interval = 1000  
    $timer1.add_tick({  
        $script:ts1=$ts1.Subtract($1second)  
        $label.Text = $ts1.ToString('hh\:mm\:ss')  
        if($ts1.Ticks -le 0){  
            $timer1.Stop()  
        }  
    })  
    $timer1.Start()  
    

    Best Regards,
    Ian

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.