none
Press any key to cancel timer RRS feed

  • Question

  • I am looking to write a powershell script that uses a timer object.  When the elapsed event occurs I would like to reboot the machine.  However, if the users presses any key before the elapsed event occurs, I want to stop the timer and abort the shutdown.

    I have the timer and intervals created ok up until now.  Where I am getting stuck is actually rebooting the machine when the evnet rises and also giving the user the ability to cancel.

    Here is what I have so far:

    $computername = hostname
    $waitTime = 15000
    
    $rebootTime = Get-Date
    #$rebootTime.addMinutes($waitTime / 60000)
    
    $timer = New-Object System.Timers.Timer 
    $timer.Interval = $waitTime 
    
    $timer.Start
    
    Write-Host "Your Machine Will Reboot At: " + $rebootTime
    Write-Host "Press any key to cancel reboot..."
    
    Register-ObjectEvent -InputObject $timer -EventName elapsed -SourceIdentifier theTimer -Action {Write-Host "Restarting Computer"}

    Thanks in advance!

    sb

    Thursday, February 23, 2012 8:11 PM

Answers

  • Here is a complete working example.

    $timer = New-Object System.Timers.Timer 
    $timer.Interval = 5000
    $timer.Start()
    $evt=Register-ObjectEvent  `
         -InputObject $timer `
         -EventName elapsed `
         -SourceIdentifier theTimer `
         -Action {Write-Host 'Restarting Computer'}
    $host.ui.rawui.FlushInputBuffer()
    while(-not $host.UI.RawUI.KeyAvailable){}
    $evt.StopJob()
    remove-job $evt
    rv timer


    ¯\_(ツ)_/¯

    • Marked as answer by stephenmbell Friday, February 24, 2012 3:56 PM
    Friday, February 24, 2012 4:49 AM

All replies

  • I am looking to write a powershell script that uses a timer object.  When the elapsed event occurs I would like to reboot the machine.  However, if the users presses any key before the elapsed event occurs, I want to stop the timer and abort the shutdown.

    I have the timer and intervals created ok up until now.  Where I am getting stuck is actually rebooting the machine when the evnet rises and also giving the user the ability to cancel.

    Here is what I have so far:

    $computername = hostname
    $waitTime = 15000
    
    $rebootTime = Get-Date
    #$rebootTime.addMinutes($waitTime / 60000)
    
    $timer = New-Object System.Timers.Timer 
    $timer.Interval = $waitTime 
    
    $timer.Start
    
    Write-Host "Your Machine Will Reboot At: " + $rebootTime
    Write-Host "Press any key to cancel reboot..."
    
    Register-ObjectEvent -InputObject $timer -EventName elapsed -SourceIdentifier theTimer -Action {Write-Host "Restarting Computer"}

    Thanks in advance!

    This waits on a keystroke.

    $evt=Register-ObjectEvent `
          -InputObject $timer '
          -EventName elapsed '
          -SourceIdentifier theTimer '
          -Action {Write-Host 'Restarting Computer'}
    Write-Host 'Your Machine Will Reboot At:$rebootTime'
    Write-Host 'Press any key to cancel reboot...'
    while(-not $host.UI.RawUI.KeyAvailable){}
    Unregister-Event $evt


    ¯\_(ツ)_/¯


    • Edited by jrv Thursday, February 23, 2012 8:52 PM
    Thursday, February 23, 2012 8:51 PM
  • I made these changes to my script but I don't seem to understand what it is trying to do - it isn't working correctly.

    First -  if I wait 15 seconds, the event never fires (I never see "Restarting Computer..." on the screen).  If I hit a key I get an unregister-event error: Event Subscription with identifier 'System.Management.Automation.PSEventJob does not exist.

    Also - If I run it the first time it does not error registering the event, if I run again it errors saying that there is already a subscription - so I added an unregister-event before the register - this doesn't seem like the way it should work.  

    I think I am just confused...
    Friday, February 24, 2012 4:31 AM
  • You forgot to start the timer.

    YOu asked about how to stop on a keystroke not about how to run a timer.

     I assumed you had the timer working.


    ¯\_(ツ)_/¯

    Friday, February 24, 2012 4:35 AM
  • Here is a complete working example.

    $timer = New-Object System.Timers.Timer 
    $timer.Interval = 5000
    $timer.Start()
    $evt=Register-ObjectEvent  `
         -InputObject $timer `
         -EventName elapsed `
         -SourceIdentifier theTimer `
         -Action {Write-Host 'Restarting Computer'}
    $host.ui.rawui.FlushInputBuffer()
    while(-not $host.UI.RawUI.KeyAvailable){}
    $evt.StopJob()
    remove-job $evt
    rv timer


    ¯\_(ツ)_/¯

    • Marked as answer by stephenmbell Friday, February 24, 2012 3:56 PM
    Friday, February 24, 2012 4:49 AM
  • If it is just about rebooting the computer you can also use this, annoying popups about System shutdown will appear though.

    cmd /c shutdown -r -t 60
    write-host "Press any key to abort reboot"
    $key = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
    cmd /c shutdown -a

    Friday, February 24, 2012 6:47 AM
    Moderator
  • If it is just about rebooting the computer you can also use this, annoying popups about System shutdown will appear though.

    cmd /c shutdown -r -t 60
    write-host "Press any key to abort reboot"
    $key = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
    cmd /c shutdown -a

    That seems strange to do in POwerShell.

    Restart-Computer does it all with no popups.

    What the OP is trying to do is schedule a reboot with an ability to cancel.

    I would just prompt -"Do you want to reboot now? (Y/N)"

    Why be extra fancy for something so trivial.


    ¯\_(ツ)_/¯

    Friday, February 24, 2012 8:06 AM
  • A little background - this script is a scheduled task that will run nightly at 2:30 AM in retail locations on POS terminals.  90% of the time, no one will be in the store at this time which means no one would be around to confirm that they want to shut down.  That other 10% of the time (the what-if's), I need the ability to cancel just in case they are in the middle of a transaction or reporting function.  When rebooting the machine can cause us to possibly lose sales when something as simple as "Press any key to cancel" can remedy this.

    Thanks for your posts, I will test it up in my lab and mark as answer when I confirm it works.

    sb

    Friday, February 24, 2012 2:34 PM
  • Did you ever get this script working?  I'm looking for one to do the same thing.  I use PS shutdown that comes with PSTools for Our WinXP machines, but it seems to have troubles with Windows 7 UAC despite using the switch to use an elevated token.  I've been looking for a way to do this with powershell.  There have not been any satisfying answers in any of the forums.

    Thanks,

    Shane

    Saturday, May 12, 2012 9:16 PM
  • Yes - it is the one marked as an answer.


    ¯\_(ツ)_/¯

    Saturday, May 12, 2012 10:04 PM