This script is used to Schedule automatic reboot using task schedule
Edit parameters
daily="DAILY" #Eg:DAILY,MON,TUE,WED,THU,FRI,SAT,SUN
time="20:00" #TIME-should be in 24 hour format
Note:
1. Run the script as a Local system user
2. Change the parameter of variable "process" = 1 to start the scheduled task.
3. Change the parameter of variable "process" = 2 to stop the scheduled task.
daily="DAILY" #Eg:DAILY,MON,TUE,WED,THU,FRI,SAT,SUN
time="20:00" #TIME-should be in 24 hour format
process=itsm.getParameter('process_value')
import os
import subprocess
import socket
from ctypes import *
class disable_file_system_redirection:
_disable = windll.kernel32.Wow64DisableWow64FsRedirection
_revert = windll.kernel32.Wow64RevertWow64FsRedirection
def __enter__(self):
self.old_value =c_long()
self.success = self._disable(byref(self.old_value))
def __exit__(self, type, value, traceback):
if self.success:
self._revert(self.old_value)
command='shutdown -r -f'
def command1(daily,time,command):
path=os.environ['PROGRAMDATA']
command='"'+command+'"'
if os.path.exists(path):
os.chdir(path)
with open("reboot_command.bat","w+") as f:
f.write('start cmd.exe /k '+command)
f.close()
file_path = os.environ['PROGRAMDATA'] + r'\reboot_command.bat'
print file_path
cmd='schtasks /ru "SYSTEM" /create /tn:reboot /tr:'+file_path+" /sc:"+daily+" /st:"+time+" /f"
with disable_file_system_redirection():
ping = subprocess.Popen(cmd,stdout=subprocess.PIPE,stderr = subprocess.PIPE,shell=True)
out = ping.communicate()
if len(out[0])>0:
print "Task Scheduler Scheduled to restart system at "+time
else:
print "Error in task scheduler.Please check your parameters"
if process == 1:
command1(daily,time,command)
else:
path=os.environ['PROGRAMDATA']
paths = path+"\\reboot_command.bat"
if os.path.exists(paths):
cmd_1 = subprocess.Popen("Schtasks /delete /TN reboot /f",stdout=subprocess.PIPE,stderr = subprocess.PIPE,shell=True)
res,err = cmd_1.communicate()
if err:
os.remove(paths)
print("Task doesn't exists")
else:
os.remove(paths)
print("Reboot Schedule task removed from task Scheduler")
else:
cmd_2 = subprocess.Popen("Schtasks /delete /TN reboot /f",stdout=subprocess.PIPE,stderr = subprocess.PIPE,shell=True)
res_2,err_2 = cmd_2.communicate()
if err_2:
print("The specified task name reboot does not exist in the system")
else:
print("Reboot Schedule task removed from task Scheduler")
Comments