Service_name = "DNS" # give here the service name

Days = "SAT,SUN" # give here the days. for example: MON,TUE,WED,THU,FRI,SAT,SUN

Time = "19:00" # give the time here in 24 hours format.

import os
from subprocess import PIPE, Popen
import ctypes

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)

def ecmd(command):   
    from subprocess import Popen, PIPE
    import ctypes
    
    with disable_file_system_redirection():
        obj = Popen(command, shell = True, stdout = PIPE, stderr = PIPE)
    out, err = obj.communicate()
    ret=obj.returncode
    if ret==0:
        print("scheduled task has been successfully created")
        if out:
            print(out.strip())
        else:
            print(ret)
    else:
        print("couldn't create a scheduled task")
        if err:
            print(err.strip())
        else:
            print(ret)

if " " in Service_name:
    taskname = "_".join(Service_name.split()) + "_Service_Recycle"
else:
    taskname = Service_name + "_Service_Recycle"

ecmd(('schtasks /ru "SYSTEM" /create /sc weekly /d %s /tn "%s" /tr "powershell '+r'\"Restart-Service '+"'%s'"+r' -Force\"'+'" /st %s /f')%(Days,taskname,Service_name,Time))
