dahall / TaskScheduler

Provides a .NET wrapper for the Windows Task Scheduler. It aggregates the multiple versions, provides an editor and allows for localization.
MIT License
1.21k stars 191 forks source link

Windows 10 security and a few changes on example code (remote) #849

Closed erniealega closed 4 years ago

erniealega commented 4 years ago

I tried to follow the example in the code in the Readme.md because it actually is what I need on my project. However, I experience a lot of issues with (1)Windows 10 security, and (2) a very few missing information on the example.

First, With Windows 10. I just wanted to share the required inbound rules to be enabled. But then again, I am no expert, and there might be missing information.

Next, I followed this:

`using (TaskService ts = new TaskService(@"\RemoteServer", "username", "domain", ""password)) { // Create a new task definition and assign properties TaskDefinition td = ts.NewTask(); td.RegistrationInfo.Description = "Does something";

     // Create a trigger that will fire the task at this time every other day
     td.Triggers.Add(new DailyTrigger { DaysInterval = 2 });

     // Create an action that will launch Notepad whenever the trigger fires
     td.Actions.Add(new ExecAction("notepad.exe", "c:\\test.log", null));

     // Register the task in the root folder
     ts.RootFolder.RegisterTaskDefinition(@"Test", td);
  }`

.. and cool. I like how it was structured. Very readable. Unfortunately, I got an exception of {42,4}Userid: with the following stack trace: at Microsoft.Win32.TaskScheduler.V2Interop.ITaskFolder.RegisterTaskDefinition(String Path, ITaskDefinition pDefinition, Int32 flags, Object UserId, Object password, TaskLogonType LogonType, Object sddl) at Microsoft.Win32.TaskScheduler.TaskFolder.RegisterTaskDefinition(String path, TaskDefinition definition, TaskCreation createType, String userId, String password, TaskLogonType logonType, String sddl) at Microsoft.Win32.TaskScheduler.TaskFolder.RegisterTaskDefinition(String path, TaskDefinition definition) .. Hmm.. not sure what happened there, eh? =)

Anyway, I tried to check ITaskFolder.RegisterTaskDefinition and it seems userId (or username) is required.. if, I'm not mistaken. So... I changed my code to the following:

`using (TaskService ts = new TaskService(@"\RemoteServer", "username", "domain", ""password)) { // Create a new task definition and assign properties TaskDefinition td = ts.NewTask(); td.RegistrationInfo.Description = "Does something";

     // Create a trigger that will fire the task at this time every other day
     td.Triggers.Add(new DailyTrigger { DaysInterval = 2 });

     // Create an action that will launch Notepad whenever the trigger fires
     td.Actions.Add(new ExecAction("notepad.exe", "c:\\test.log", null));

     // Register the task in the root folder
     ts.RootFolder.RegisterTaskDefinition(@"Test", td, TaskCreation.Create, "username");
  }`

and voila! works like charm! Kudos for this wrapper, man.

BTW, I'm running .NET Core 3.1

dahall commented 4 years ago

Thank you. I have updated the sample code and wiki page per your suggestions.