我想创建一个使用消息框显示特定事件的闹钟。
使用提供的代码:
[System.Windows.Forms.MessageBox]::Show("Do this task" , "Alert!")
Do
{
$waitMinutes = 1
$startTime = get-date
$endTime = $startTime.addMinutes($waitMinutes)
$timeSpan = new-timespan $startTime $endTime
Start-Sleep $timeSpan.TotalSeconds
# Play System Sound
[system.media.systemsounds]::Exclamation.play()
# Display Message
Show-MessageBox Reminder "Do this task."
}
# Loop until 11pm
Until ($startTime.hour -eq 23)
答案 0 :(得分:2)
我认为使用事件而不是循环是一种更酷的方法。
[datetime]$alarmTime = "November 7, 2013 10:30:00 PM"
$nowTime = get-date
$tsSeconds = ($alarmTime - $nowTime).Seconds
$timeSpan = New-TimeSpan -Seconds $tsSeconds
$timer = New-Object System.Timers.Timer
Register-ObjectEvent -InputObject $timer -EventName Elapsed -Action { [System.Windows.Forms.MessageBox]::Show("Brush your Teeth" , "Alert!") }
$timer.Autoreset = $false
$timer.Interval = $timeSpan.TotalMilliseconds
$timer.Enabled = $true
我真的没有心情为你写一个完整的解决方案,因为那会有效,而且我不在工作,但我想在这里所有的答案中你已经得到了你需要的一切。
我引用了此页面以获取有关上述内容的指导:
答案 1 :(得分:0)
试试这个:
function New-Alarm
{
param(
[Parameter(Mandatory=$true,HelpMessage="Enter a time in HH:MM format (e.g. 23:00)")]
[String]
$time,
[Parameter(Mandatory=$true,HelpMessage="Enter the alert box title (e.g. Alert!).")]
[String]
$alertBoxTitle,
[Parameter(Mandatory=$true,HelpMessage="Enter the alert message.")]
[String]
$alertBoxMessage
)
do
{
Start-Sleep -Seconds 1
}
until((get-date) -ge (get-date $time))
# Play system sound:
[system.media.systemsounds]::Exclamation.play()
# Display message
[System.Windows.Forms.MessageBox]::Show($alertBoxMessage,$alertBoxTitle)
}
New-Alarm -time "22:00" -alertBoxTitle "Alert!" -alertBoxMessage "Time to study PowerShell!"
答案 2 :(得分:0)
如果您使用V3,我会推荐这个(来自提升/管理员提示):
$principal = New-ScheduledTaskPrincipal -LogonType Interactive
Register-ScheduledJob -Name BrushTeeth -Trigger @{Frequency='Daily';At="7:30am"} -ScriptBlock {
Add-Type -assembly System.Windows.Forms
[Windows.Forms.MessageBox]::Show('Brush your teeth!')}
Set-ScheduledTask -TaskName "\Microsoft\Windows\PowerShell\ScheduledJobs\BrushTeeth" -Principal $principal
真的是人,PowerShell V4现在已经出局了。现在是时候至少转向V3了。 :-)
注意:$ principal业务需要启用“仅在用户登录时运行”设置。这允许UI与桌面交互。如果没有这个,现在会出现消息框。