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)
If you were on V3 I would recommend this (from an elevated/admin prompt):
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.
Try this:
I think using an event rather than a loop is a much cooler way to do this.
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