c#:以不同的用户身份创建 windows 计划任务 [英] c# : Create windows scheduled task as a different user

查看:42
本文介绍了c#:以不同的用户身份创建 windows 计划任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用内置的 TaskService 和 TaskDefinition 库从 c# 动态创建 Windows 计划任务.

I'm creating Windows Scheduled Tasks dynamically from c# using the build-in TaskService and TaskDefinition libraries.

但对于其中一些,我们需要创建然后以不同的用户(本地服务或网络服务)运行.由于任务是动态创建和删除的,我们无法手动编辑所有任务来更改用户.我们需要通过代码来完成.有可能吗?

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?

我尝试了以下设置:

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.

推荐答案

我已经尝试了一些 Task Scheduler 的东西,并复制了您的问题.我相信我已经找到了一些东西,也许它们可以提供帮助.

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. 首先,如果您使用服务帐户在调试器中创建任务,您需要确保您的 Visual Studio 或其他 IDE 以管理员身份启动,以确保您拥有执行此操作的正确权限任务.

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.

2. 我不确定您是否在稍后的代码中执行此操作,但为了使任务保存并作为 NETWORK SERVICE 运行,我必须确定 Network Service作为 NT AUTHORITY\\NETWORKSERVICE 在原理和 RegisterTaskDefinition 方法上:

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:

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:

我猜测以上两点或其中之一可能已阻止添加任务,希望对您有所帮助

I'm guessing that one or both of the above points may have stopped the task from being added, hope that helps

这篇关于c#:以不同的用户身份创建 windows 计划任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆