Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

problem with Task when starting from a service #973

Closed
Slavok47 opened this issue Aug 1, 2023 · 4 comments
Closed

problem with Task when starting from a service #973

Slavok47 opened this issue Aug 1, 2023 · 4 comments

Comments

@Slavok47
Copy link

Slavok47 commented Aug 1, 2023

I can not figure out the launch and services, tell me what the problem is

using (TaskService service = new TaskService())
            {
                string taskName = "YourScheduled";              
                    var task = service.NewTask();
                    task.RegistrationInfo.Description = "YourScheduledTaskDescription";
                    task.RegistrationInfo.Author = "YourScheduled";
                    task.Actions.Add(new ExecAction(@"C:\Windows\notepad.exe"));
                    TaskService.Instance.RootFolder.RegisterTaskDefinition(taskName, task).Run(); 
            }

when using WinForm everything is fine, the service is used from LocalSystem

@dahall
Copy link
Owner

dahall commented Aug 1, 2023

Check info at #494

@Slavok47
Copy link
Author

Slavok47 commented Aug 2, 2023

I don't think it's a permissions issue, the error is in the line
TaskService service = new TaskService();

I tried to process it

        try
            {
               TaskService service = new TaskService();
            }
            catch (Exception x)
            {
                output log              
            }

but it doesn't work, I don't even understand why

@Slavok47
Copy link
Author

Slavok47 commented Aug 2, 2023

The problem is solved, now the task is created, but on behalf of the system, it needs to be run on behalf of the user logged into the system

@Slavok47
Copy link
Author

Slavok47 commented Aug 2, 2023

Problem solved, everything works, I don't know how much is correct

ManagementObjectSearcher searcher_User = new ManagementObjectSearcher("SELECT UserName FROM Win32_ComputerSystem");
                    ManagementObjectCollection collection = searcher_User.Get();
                    string userName = (string)collection.Cast<ManagementBaseObject>().First()["UserName"];
                    TaskService.Instance.RootFolder.RegisterTaskDefinition(taskName, task, TaskCreation.CreateOrUpdate, userName, null, TaskLogonType.None).Run();

@dahall dahall closed this as completed Aug 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants