Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
powershell:倒计时计时器_Powershell_Datetime - Fatal编程技术网

powershell:倒计时计时器

powershell:倒计时计时器,powershell,datetime,Powershell,Datetime,你好,我是powershell的新手 我试图在启动时运行这个脚本,以便在系统启动时为检查预先配置一个计时器 要求: 抓取今天的日期并显示离截止日期还有多少小时 我正在使用我发现的PowerShell代码示例: Add-Type -AssemblyName System.Windows.Forms Add-Type -AssemblyName System.Drawing $Form = New-Object system.Windows.Forms.Form $script:Label = Ne

你好,我是powershell的新手

我试图在启动时运行这个脚本,以便在系统启动时为检查预先配置一个计时器

要求: 抓取今天的日期并显示离截止日期还有多少小时

我正在使用我发现的PowerShell代码示例:

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 exam will end in $CountDown hrs"
        $script:CountDown--
    }
)
$script:Timer.Start()
$script:Form.ShowDialog()
我试图格式化也有显示为

$clock = Get-Date $myDateTime  -Format HH:mm:ss.fff

但是它只抓取秒数,我如何按日期时间或格式HH:mm:ss进行倒计时。fff

我添加了一个到期日期/时间,代码从中减去当前日期/时间,然后以dd days HH:mm:ss格式格式化timespan值

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

$DueDate = Get-Date -Date "4/4/2021" -Hour 17 #Due Date/Time!

$Form  = New-Object system.Windows.Forms.Form
$Form.Width   = "500"
$Form.Height  = "100"
$lblCountDown = New-Object System.Windows.Forms.Label
$lblCountDown.AutoSize = $true
$lblCountDown.Font = "Century, 15"
$lblCountDown.Text =    
  "Your exam will end in: {0:dd} days {0:hh}:{0:mm}:{0:ss}" -f 
  ($DueDate - (Get-Date))

$Form.Controls.Add($lblCountDown)
$tmrCountdown = New-Object System.Windows.Forms.Timer
$tmrCountdown.Interval = 1000
$tmrCountdown.add_Tick(
 {
  $lblCountDown.Text = 
    "Your exam will end in: {0:dd} days {0:hh}:{0:mm}:{0:ss}" -f 
     ($DueDate - (Get-Date))
 }
)

$tmrCountdown.Start()
$Form.ShowDialog()
$tmrCountdown.Dispose()
Remove-Variable tmrCountdown

当然,如果不希望显示天数,只需从字符串中删除格式化参数

HTH

如果没有必要,不要编写UX/UI/GUI。

首先使用盒子上的东西,例如浏览器,在这个用例中

function Show-CountDownInWebPage
{
    [cmdletbinding(SupportsShouldProcess)]
    [Alias('scdw')]
    Param
    (
        [datetime]$TargetDate
    ) 

    $ie = New-Object -comobject "InternetExplorer.Application"
    $ie.navigate("about:blank")

    $ie.top     = 10
    $ie.left    = 10
    $ie.height  = 120
    $ie.width   = 260
    $ie.toolbar = $false
    $ie.visible = $true

    $body       = $script:ie.document.body.innerhtml

    while ((Get-Date) -le $TargetDate)
    {
        $TimeLeft                   = New-TimeSpan -Start (Get-Date) -End $TargetDate
        $body                       = "Time left to: $TargetDate<br> Hours: $($TimeLeft.hours) Minutes:$($TimeLeft.minutes) Seconds: $($TimeLeft.Seconds)"
        $ie.document.body.innerhtml = $body

        Start-Sleep 0.5
    }

    #Beep twice at the end.
    [console]::beep(600,400)
    [console]::beep(600,400)
}

Show-CountDownInWebPage -TargetDate (Get-Date).AddMinutes(1)
以这种方式运行-您可以创建一个快捷方式来启动此任务或使用计划任务

.\Start-ExamClock.ps1
及时回复。只需退格并覆盖

Days until the exam. Enter accept the default or modify.: 04/05/2021 14:00:00

# Results - in a normal PowerShell console host window
<#
Exam Due in : 04/06/2021 14:00:00
Days        : 3
Hours       : 10
Minutes     : 7
Seconds     : 8
#>
离考试还有几天。输入接受默认值或修改:04/05/2021 14:00:00
#结果-在正常的PowerShell控制台主机窗口中
当然,只要控制台正在运行,它就会更新。如果您发现自己在consolehost/Windows Terminal/ISE/VSCode中,您可以将其添加到标题栏中,以及您在标题栏中的其他内容,或者将其设置为自定义提示

如果您真的需要/想要编写UX/UI/GUI,那么就很酷吧,但请确保首先了解该主题的正确和行业最佳实践,并且要知道,这条道路与Powershell根本没有任何关系

Youtube是你的朋友

“Windows UX/UI最佳实践:


那么,问题是您没有给出开始的时间范围/格式。几十年来,倒计时在许多语言中都是一件事。网络上有许多预先构建的倒计时时间,使用多种语言,包括PowerShell。所以,没有理由在这件事上从头开始,也没有理由为之奋斗。理论上是个好主意。然而,假设某人的盒子上有东西是不正确的。就我个人而言,我在我的机器上禁用了IE(安全漏洞),所以脚本只会产生错误。既然OP正在编写PS,为什么不将所有内容都保存在PS中,因为这是一个基本要求?不是想在这里挑起一场火战,只是做个观察。选择,选择,选择。。。。同样,只要弹出控制台主机或现有实例的PowerShell标题栏,就可以避免所有表单内容。在工作了4年多之后,我坚信“不要不必要地增加你的工作量,在开始工作之前使用现有的东西。”然而,作为一个即将被重新尝试的极客(如果我找不到新的工作来与我现在的雇主打交道),这只是两个老人,只是为那些潜在的年轻人交换/分享经验/想法/概念而已阿门!祝你退休后一切顺利,你会喜欢的!我已经做了21年了,不知道我是怎么找到时间工作的!谢谢,罗杰,太棒了。办公室政治/不必要的会议,啊!!!
Days until the exam. Enter accept the default or modify.: 04/05/2021 14:00:00

# Results - in a normal PowerShell console host window
<#
Exam Due in : 04/06/2021 14:00:00
Days        : 3
Hours       : 10
Minutes     : 7
Seconds     : 8
#>