Reboot machine if Up time is more than 8 days


Please find the procedure to reboot machines which are turned on week ago,

import time;
import os;
import re;
import ctypes
$os = Get-WmiObject win32_operatingsystem
$uptime = (Get-Date) - ($os.ConvertToDateTime($os.lastbootuptime))
$Display = "Uptime in days: " + $Uptime.days 
Write-Output $Display 
if($uptime.days -ge 8 )
Write-Output "Time to restart the machine and Force Restart function initiated!!"
restart-computer -Force
"No Restart required"
fobj= open(file, "w");
class disable_file_system_redirection:
    _disable = ctypes.windll.kernel32.Wow64DisableWow64FsRedirection
    _revert = ctypes.windll.kernel32.Wow64RevertWow64FsRedirection
    def __enter__(self):
        self.old_value = ctypes.c_long()
        self.success = self._disable(ctypes.byref(self.old_value))
    def __exit__(self, type, value, traceback):
        if self.success:
with disable_file_system_redirection():
    out=os.popen('powershell.exe -executionpolicy bypass -file C:\checkboottime.ps1').read();

uptime_check_8_days.txt (1.12 KB)

Is there a way to modify this script to initiate restart of a workstation on a specific day of week and time of day? Also, provide GUI warning of restart and give option to over-ride. Generally this would be restarted overnight (non-production hours), but just in case. If they cancel reboot, then submit ticket?

The most important part is the reboot on specified date and time. The warning it also important, but the option to postpone the restart and submit ticket (notice) if postpone was selected is not as important, but nice if could be done.


I believe that you would set your procedure to run on your profile, on say a saturday, that is when it will do the check, and initiate the reboot.

Hi @rmorton

Yes @BOSS is correct, you can schedule any procedure as per your requirement.

Please refer

I can provide script to reboot the machine with GUI warning and postpone option. I will let you when it is completed.


I imported this into my procedures, set up my profile to run the procedure with system access, and I am getting this error:
Data: Traceback (most recent call last): File “”, line 40, in WindowsError: [Error 5] Access is denied: ‘C:\checkboottime.ps1’
Any ideas of why I get this?? I figured that the system user would have access to such files??

EDIT: I looked back, I did not import this one, since it was in a text file, I copied and pasted the script. I am now wondering if some syntax maybe off from the copy and paste?? I will have to double check this!!

EDIT_2: I did look back in the execuruin logs, and it shows that it did run on some systems.


Did you run the procedure user as 'System User ’ ? Script works well as a system user action.


Yes sir, I did set to run as system user.


Please refer updated script in the attachment.

If problem persist

WindowsError: [Error 5] Access is denied: ‘C:\checkboottime.ps1’

Please delete last line of the procedure os.remove(file) and then execute the procedure.

20170125-Reboot-machine-if-Up-time-is-more-than-8-days—update.json (1.8 KB)

Hi @rmorton,

As @mkannan and @BOSS suggested, we have published a script to get user response while attempting to reboot hence you can schedule the procedure [script] when you would like to run it periodically.

Please refer the link

To schedule the procedure:


Problem persisted, I commented out the suggested line, and will advise, but manually reran, and it finished successfully.