Quickly schedule computer restart - in the next 24 hours

DP
DP Member Posts: 56 ✭✭✭
edited December 2023 in Remote Monitoring

This script allows you to quickly schedule a computer restart within the next 24 hours - it will ask you to put in a time value (in 24 hour format), then that computer will restart at that time.

Posted in Script Library: _DP - Restart computer X

Please test all posted scripts.

Tagged:

Comments

  • kim
    kim Member Posts: 113 ✭✭✭

    Thank you for sharing @DP!! I’ll have to add this to my library and test it out in my environment.

  • dyoder
    dyoder Member Posts: 52 ✭✭✭

    @DP That's a great script!

    If I may, I would recommend the following adjustments: delete line 16 and change line 17 to be as follows:

    Start-Process "shutdown.exe" -ArgumentList "/r /t ${secondsUntilShutdown}" -Wait
    

    The reason for this is 2-fold:

    1. The script will now quickly return back to the Atera console having scheduled the reboot for some time in the future. If there was an error, this would also be returned too.
    2. Instead of waiting for the script to pause it's own execution before invoking an immediate reboot, the reboot will now be scheduled via the shutdown command. This means, if you made a mistake and need to cancel the reboot you can simply run shutdown /a from any CMD or Powershell console to abort the scheduled restart. Trust me, you will want the ability to quickly abort this action.

  • DP
    DP Member Posts: 56 ✭✭✭
    edited September 2023

    Thanks @dyoder that works great. I'm not very good at scripting and rely on AI to fill in the gaps.

    I can't see it in the script Library - can you still? Also not sure how to edit it once submitted.

    I've therefore uploaded it again with your changes: _DP - Schedule Restart X

  • DP
    DP Member Posts: 56 ✭✭✭

    Here is the script in full if not approved yet.

    Prompt user to enter time in 24 hour format
    $time = "{[Time_Value]}"
    Convert time to DateTime object
    $shutdownTime = [DateTime]::ParseExact($time, "HH:mm", $null)
    Calculate time until shutdown
    $timeUntilShutdown = $shutdownTime - (Get-Date)
    Convert time to seconds
    $secondsUntilShutdown = [int]$timeUntilShutdown.TotalSeconds
    Schedule shutdown using shutdown.exe
    if ($secondsUntilShutdown -gt 0) {
    Write-Host "Computer will Restart at $time"
    Start-Process "shutdown.exe" -ArgumentList "/r /t ${secondsUntilShutdown}" -Wait } else {
    Write-Host "Invalid time entered"
    }

  • kim
    kim Member Posts: 113 ✭✭✭
    edited September 2023

    @DP didnyou submit it to the shared script library already? Thank you for sharing too!

  • DP
    DP Member Posts: 56 ✭✭✭

    I did but it's now gone - like a newly released Atera feature 😂

  • kim
    kim Member Posts: 113 ✭✭✭

    @DP maybe try to submit it again, but make sure it has a good description so when people search in the library for it will come up quickly.

  • dyoder
    dyoder Member Posts: 52 ✭✭✭

    I'm not aware of any mechanism by which scripts submitted to the Shared Script Library can be updated. @nina or @Sarah_from_Atera can you provide any clarity here?

  • kim
    kim Member Posts: 113 ✭✭✭

    @dyoder I have seen instances where scripts that are improvements of older scripts get accepted. I would just make sure it’s in the description.

  • DP
    DP Member Posts: 56 ✭✭✭
  • nina
    nina Internal Posts: 428 ✭✭✭✭✭

    @kim ?

  • dfletcher
    dfletcher Member Posts: 30 ✭✭

    @DP This looks good.

    BTW - are you searching the script library for similar scripts before writing up your own? I found this from Apr 26, 2022:

  • DP
    DP Member Posts: 56 ✭✭✭

    Thanks @dfletcher I did see that later on. I found an issue with the script that perhaps @dyoder could fix. Wanting to restart a computer at 1am, when it is say 11pm doesn't work. I usually use it to schedule restarts later in the day so that's usually not an issue.

  • dyoder
    dyoder Member Posts: 52 ✭✭✭
    edited September 2023

    @DP you just need to add this:

    # If the current time is past the time specified, add 1 day to $shutdownTime
    If ( $shutdownTime -lt (Get-Date) ) { $shutdownTime = $shutdownTime.AddDays(1) }
    

    And insert it just before this:

    # Calculate time until shutdown
    $timeUntilShutdown = $shutdownTime - (Get-Date)

    If you want the console output to be a little more detailed about when the restart will occur, change the Write-Host line just before the Start-Process command to be as follows:

    Write-Host "Restart has been scheduled for ${shutdownTime}"
    

    I made a few other very minor tweaks. Here's the full script:

    # Prompt user to enter time in 24 hour format
    $time = "{[Time_Value]}"
    # Convert time to DateTime object Try { $shutdownTime = [DateTime]::ParseExact($time, "HH:mm", $null) }
    Catch { Write-Error "Invalid time entered!" ; Exit }
    # If the current time is past the time specified, add 1 day to $shutdownTime If ( $shutdownTime -lt (Get-Date) ) { $shutdownTime = $shutdownTime.AddDays(1) }
    # Calculate time until shutdown $timeUntilShutdown = $shutdownTime - (Get-Date)
    # Convert time to seconds $secondsUntilShutdown = [int]$timeUntilShutdown.TotalSeconds
    # Schedule shutdown using shutdown.exe Start-Process "shutdown.exe" -ArgumentList "/r /t ${secondsUntilShutdown}" -Wait
    Write-Output "Restart has been scheduled for ${shutdownTime}"

  • dfletcher
    dfletcher Member Posts: 30 ✭✭

    This seems to work well. I have a script I call cancelreboot.ps1 with only shutdown /a in it. Gives me a quick way to undo if needed. 😁

  • kim
    kim Member Posts: 113 ✭✭✭

    These look great! I wonder if we can make a suggestion with the shared script library for edits so that the newer ones could replace older ones we had before. Maybe in the description when you submit it, you can put a note in there that it improves [insert script name] so that it'll let their librarians maybe replace old ones.

  • DP
    DP Member Posts: 56 ✭✭✭

    Thanks for all of your input and suggestions everyone. @dyoder can't wait to see how you improve the software installation alert system. I'll get that up soon.

  • nina
    nina Internal Posts: 428 ✭✭✭✭✭
  • DP
    DP Member Posts: 56 ✭✭✭
    edited October 2023

    This is a great improvement! I hope it works. Nice to see this as well.

    Would like more flexibility on the time - only can select every 30 min could be annoying.

    This script is still useful for the mobile app which doesn't have this feature.

  • nina
    nina Internal Posts: 428 ✭✭✭✭✭

    Agreed. What increments would you like to see?

  • DP
    DP Member Posts: 56 ✭✭✭

    It would be ideal to put in the time we want.

  • nina
    nina Internal Posts: 428 ✭✭✭✭✭
    edited October 2023

    Understood. You can enter the time you want, in 30 min. increments.

    If that's not what you want, then perhaps it would be a good idea to add it to UserVoice.

  • @nina

    @DP was saying we should be able to put any exact time in that we want - not restricted to 30 minute increments ! ditch the drop down and let us just type the time in! (or put a proper time picker in)

    Sorry, but I do think in this instance suggesting that is a feature request is a little bit patronising when this was just poor UX design in the first place.

    Either that, or perhaps you should revise your next marketing campaign to ….

    "Come use atera for all you IT automation needs" ** (as long as you only need them done on the exact half hour)🤣

  • nina
    nina Internal Posts: 428 ✭✭✭✭✭

    Hi @nathan - Understood. I will relay this information to our Product Team.