Reboot machine if Up time is more than 8 days

Hi

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

import time;
import os;
import re;
import ctypes
file='C:\checkboottime.ps1';
input="""
$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
}
Else
{
"No Restart required"
}
"""
fobj= open(file, "w");
fobj.write(input);
fobj.close();
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:
            self._revert(self.old_value)
    
with disable_file_system_redirection():
    out=os.popen('powershell.exe -executionpolicy bypass -file C:\checkboottime.ps1').read();
    print(out);
os.remove("C:\checkboottime.ps1")

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.

Thanks

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

https://forum.mspconsortium.com/forum/products/other-comodo-products/comodo-device-management/wiki-faq-how-to/7141-wiki-how-to-schedule-the-procedure-using-profiles

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

Thanks,
Kannan

@mkannan
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.

Hi @BOSS

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

Thanks,
Kannan

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

Hi @BOSS

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 https://forum.mspconsortium.com/forum/script-library/8134-reboot-machine-with-user-interaction.

To schedule the procedure: https://forum.mspconsortium.com/forum/products/other-comodo-products/comodo-device-management/wiki-faq-how-to/7141-wiki-how-to-schedule-the-procedure-using-profiles

Thanks,
Purushoth

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