How to make a messagebox appear at a specific time in PowerShell?

别等时光非礼了梦想. 提交于 2019-12-11 13:12:12

问题


I want to create an alarm clock that displays a specific event using a messagebox.

Using the code provided:

 [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)

回答1:


I think using an event rather than a loop is a much cooler way to do this.

[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

I'm not really in the mood to write you a complete solution because that would be work, and I'm not at work, but I think between all the answers here you've got everything you need.

I referenced this page for guidance on the above:

http://blogs.technet.com/b/heyscriptingguy/archive/2011/06/16/use-asynchronous-event-handling-in-powershell.aspx




回答2:


Try this:

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!"



回答3:


If you were on V3 I would recommend this (from an elevated/admin prompt):

$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

Really folks, PowerShell V4 is out now. It's time to move to at least V3. :-)

Note: the $principal business is required to enable the setting "Run only when user is logged on". This allows the UI to interact with the desktop. Without this, now message box appears.



来源:https://stackoverflow.com/questions/19846692/how-to-make-a-messagebox-appear-at-a-specific-time-in-powershell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!