首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在Powershell消息框中获取计时器?

如何在Powershell消息框中获取计时器?
EN

Stack Overflow用户
提问于 2016-08-09 21:00:00
回答 2查看 15.7K关注 0票数 2

我正在尝试在我的消息框中显示一个计时器,这是我用PS表单创建的。我想要这样的东西:

“你的电脑将在10秒内关机”,在1秒后。

“您的电脑将在9秒后关机”

“你的电脑将在8秒内关机”等等。

希望你能帮助我。

EN

回答 2

Stack Overflow用户

发布于 2016-08-11 02:26:22

我看不到刷新消息框中文本的方法。如果我必须这样做,我可能会弹出另一个带有标签的表单,并使用一个计时器来刷新每个刻度上的标签文本。

下面是一个使用潜在起点的代码示例:

代码语言:javascript
运行
复制
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$Form = New-Object system.Windows.Forms.Form
$script:Label = New-Object System.Windows.Forms.Label
$script:Label.AutoSize = $true
$script:Form.Controls.Add($Label)
$Timer = New-Object System.Windows.Forms.Timer
$Timer.Interval = 1000
$script:CountDown = 60
$Timer.add_Tick(
    {
        $script:Label.Text = "Your system will reboot in $CountDown seconds"
        $script:CountDown--
    }
)
$script:Timer.Start()
$script:Form.ShowDialog()

您将需要扩展以满足您的需求,例如,当倒计时到0时,条件逻辑可以执行任何您想要的操作(例如,重新启动),也许可以添加一个中止按钮,等等。

票数 3
EN

Stack Overflow用户

发布于 2016-08-11 00:09:59

有来自Windows Script Host的PopUp Method,它可以让你为PopUp设置一个time to live。我不认为有办法不刷新来自PowerShell的消息框(不要引用我的话)。代码中有两行来自here

不确定这是否是你想要的,但这是可行的(粗糙的解决方案):

代码语言:javascript
运行
复制
$timer = New-Object System.Timers.Timer
$timer.AutoReset = $true #resets itself
$timer.Interval = 1000 #ms
$initial_time = Get-Date
$end_time = $initial_time.AddSeconds(12) ## don't know why, but it needs 2 more seconds to show right

# create windows script host
$wshell = New-Object -ComObject Wscript.Shell
# add end_time variable so it's accessible from within the job
$wshell | Add-Member -MemberType NoteProperty -Name endTime -Value $end_time

Register-ObjectEvent -SourceIdentifier "PopUp Timer" -InputObject $timer -EventName Elapsed -Action {
    $endTime = [DateTime]$event.MessageData.endTime
    $time_left = $endTime.Subtract((Get-Date)).Seconds

    if($time_left -le 0){
        $timer.Stop()
        Stop-Job -Name * -ErrorAction SilentlyContinue
        Remove-Job -Name * -ErrorAction SilentlyContinue
        #other code
        # logoff user?
    }
    $event.MessageData.Popup("Your PC will be shutdown in $time_left sec",1,"Message Box Title",64)
} -MessageData $wshell

$timer.Start()

编辑:@JonDechiro提出的解决方案比我的干净得多,也更适合OP的要求。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38851482

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档