It's Windows 7.
The entire code is below:
using (TaskService ts = new TaskService()) { // Create a new task definition and assign properties if (schday == "ALL") { try { string userId = string.Concat(Environment.UserDomainName, "\\", Environment.UserName); TaskDefinition tdall = ts.NewTask(); userId = string.Concat(Environment.UserDomainName, "\\", Environment.UserName); tdall.RegistrationInfo.Description = schtask.ToString() + i.ToString(); tdall.Principal.LogonType = TaskLogonType.S4U; tdall.Principal.RunLevel = TaskRunLevel.Highest; tdall.Principal.UserId = userId; DailyTrigger dtall = new DailyTrigger(); // Add a trigger that will fire the task at this time every day dtall = (DailyTrigger)tdall.Triggers.Add(new DailyTrigger { DaysInterval = 1 }); dtall.StartBoundary = schtime; tdall.Actions.Add(new ExecAction("psshutdown.exe", "-d -accepteula", @"C:\EnergyWatchIT\App_Files\")); i++; string taskName = "EWITSchedSleepAll" + schtask.ToString() + i.ToString(); ts.RootFolder.RegisterTaskDefinition(taskName, tdall, TaskCreation.CreateOrUpdate, userId, LogonType: TaskLogonType.S4U); } catch (Exception j) { Logger.append("j is " + j.Message + Environment.NewLine + j.StackTrace, Logger.ALL); MessageBox.Show("j is " + j.ToString()); } try { string userId = string.Concat(Environment.UserDomainName, "\\", Environment.UserName); TaskDefinition tdall = ts.NewTask(); userId = string.Concat(Environment.UserDomainName, "\\", Environment.UserName); tdall.RegistrationInfo.Description = schtask.ToString() + i.ToString(); tdall.Principal.LogonType = TaskLogonType.S4U; tdall.Principal.RunLevel = TaskRunLevel.Highest; tdall.Principal.UserId = userId; DailyTrigger dtall = new DailyTrigger(); // Add a trigger that will fire the task at this time every day dtall = (DailyTrigger)tdall.Triggers.Add(new DailyTrigger { DaysInterval = 1 }); dtall.StartBoundary = schtime; tdall.Actions.Add(new ExecAction("psshutdown.exe", "-d -accepteula", @"C:\EnergyWatchIT\App_Files\")); i++; string taskName = "EWITSchedSleepAll" + schtask.ToString() + i.ToString(); ts.RootFolder.RegisterTaskDefinition(taskName, tdall, TaskCreation.CreateOrUpdate, userId, LogonType: TaskLogonType.S4U); } catch (Exception j) { Logger.append("j is " + j.Message + Environment.NewLine + j.StackTrace, Logger.ALL); MessageBox.Show("j is " + j.ToString()); }
static void sleepmethod(String schtask, String schday, DateTime schtime, int i) { try { using (TaskService ts = new TaskService()) { // Create a new task definition and assign properties if (schday == "ALL") { try { string userId = string.Concat(Environment.UserDomainName, "\\", Environment.UserName); MessageBox.Show("In try"); TaskDefinition tdall = ts.NewTask(); userId = string.Concat(Environment.UserDomainName, "\\", Environment.UserName); MessageBox.Show(userId); tdall.RegistrationInfo.Description = schtask.ToString() + i.ToString(); tdall.Principal.LogonType = TaskLogonType.S4U; tdall.Principal.GroupId = "Users"; //tdall.Principal.RunLevel = TaskRunLevel.Highest; tdall.Principal.UserId = userId; DailyTrigger dtall = new DailyTrigger(); // Add a trigger that will fire the task at this time every day dtall = (DailyTrigger)tdall.Triggers.Add(new DailyTrigger { DaysInterval = 1 }); dtall.StartBoundary = schtime; tdall.Actions.Add(new ExecAction("psshutdown.exe", "-d -accepteula", @"C:\EnergyWatchIT\App_Files\")); i++; string taskName = "EWITSchedSleepAll" + schtask.ToString() + i.ToString(); ts.RootFolder.RegisterTaskDefinition(taskName, tdall, TaskCreation.CreateOrUpdate, "NETWORK SERVICE", LogonType: TaskLogonType.Group); } catch (Exception j) { Logger.append("j is " + j.Message + Environment.NewLine + j.StackTrace, Logger.ALL); MessageBox.Show("j is " + j.ToString()); } } } }