#To define a particular parameter, replace the 'parameterName' inside itsm.getParameter('parameterName') with that parameter's name
daily=itsm.getParameter('restart_frequency')   #Eg:DAILY,MON,TUE,WED,THU,FRI,SAT,SUN
time=itsm.getParameter('time')    #TIME-should be in 24 hour format

import os
import subprocess
import ctypes
from ctypes import *

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)

command='shutdown -r ' 
def command1(daily,time,command):
    path=os.environ['PROGRAMDATA']
    command='"'+command+'"'
    if  os.path.exists(path):
        os.chdir(path)
        with open("command.bat","w+") as f:
            f.write('start cmd.exe /k '+command)
            f.close()
            
    with disable_file_system_redirection():
        out=os.popen('netsh int ip reset').read()
        print(out)
        res = subprocess.Popen('schtasks  /ru "SYSTEM"  /create /tn:Restart /tr:'+path+"\command.bat /sc:"+daily+" /st:"+time+" /f",stdout=subprocess.PIPE,stderr = subprocess.PIPE,shell=True)
        out = res.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"


command1(daily,time,command)
