简体   繁体   English

c#:以不同的用户身份创建 windows 计划任务

[英]c# : Create windows scheduled task as a different user

I'm creating Windows Scheduled Tasks dynamically from c# using the build-in TaskService and TaskDefinition libraries.我正在使用内置的 TaskService 和 TaskDefinition 库从 c# 动态创建 Windows 计划任务。

But for some of them, we need to create then to run as a different user (Local Service or Network Service).但是对于其中一些,我们需要创建然后以不同的用户(本地服务或网络服务)运行。 As the tasks are created and removed dynamically we cannot edit all of them manually to change the user.由于任务是动态创建和删除的,我们无法手动编辑所有任务来更改用户。 We need to do it via code.我们需要通过代码来完成。 Is is possible?有可能吗?

I've tried the following settings:我尝试了以下设置:

TaskDefinition.Principal.Id = "NETWORK SERVICE";
TaskDefinition.Principal.LogonType = TaskLogonType.ServiceAccount;

but this gives me the very descript error when creating the task:但这在创建任务时给了我一个非常描述性的错误:

System.Runtime.InteropServices.COMException: '(52,4):Task:' System.Runtime.InteropServices.COMException: '(52,4):Task:'

Without those 2 lines, it works but creates them as the logged in user.如果没有这两行,它可以工作,但会以登录用户的身份创建它们。

I've played around with the Task Scheduler stuff a bit and have replicated your problem.我玩过任务计划程序的东西并复制了您的问题。 I believe I've found some things out, maybe they can help.我相信我已经找到了一些东西,也许它们可以提供帮助。

1. Firstly if your making Tasks in the debugger using Services Accounts, you'll want to ensure your Visual Studio or other IDE is launched as administrator to ensure you have the correct privileges to do this task. 1.首先,如果您使用服务帐户在调试器中创建任务,您需要确保您的 Visual Studio 或其他 IDE 以管理员身份启动,以确保您拥有执行此任务的正确权限。

2. I'm not sure if you do this later in your code but to make the task save and run as NETWORK SERVICE, I had to Identify Network Service as NT AUTHORITY\\\\NETWORKSERVICE in both the principle and on the RegisterTaskDefinition method: 2.我不知道,如果你这样做后面的代码反而使保存任务并运行了网络服务,我必须确定Network ServiceNT AUTHORITY\\\\NETWORKSERVICE中并重的原则,并在RegisterTaskDefinition方法:

TaskService tService = new TaskService();
TaskDefinition tDefinition = tService.NewTask();
tDefinition.Principal.Id = "NT AUTHORITY\\NETWORKSERVICE";
tDefinition.Principal.LogonType = TaskLogonType.ServiceAccount;
tDefinition.RegistrationInfo.Description = "Testing";

tDefinition.Triggers.Add(new DailyTrigger {DaysInterval = 2});
tDefinition.Actions.Add(new ExecAction("notepad.exe"));
tService.RootFolder.RegisterTaskDefinition(@"Test", tDefinition, TaskCreation.CreateOrUpdate, 
                                            "NT AUTHORITY\\NETWORKSERVICE", null, 
                                            TaskLogonType.ServiceAccount);

I used the above code to make a test Task that got successfully added to my scheduler as Network Service as shown below:我使用上面的代码制作了一个测试任务,该任务作为Network Service成功添加到我的调度程序中,如下所示: 在此处输入图片说明

I'm guessing that one or both of the above points may have stopped the task from being added, hope that helps我猜以上两点可能已经阻止了任务的添加,希望有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM