如何通过WMI在管理员用户中创建计划任务 [英] How to create scheduled task in administrator user through WMI

查看:268
本文介绍了如何通过WMI在管理员用户中创建计划任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好

我正在通过代码安排任务.默认情况下,运行方式"用户为
NT AUTHORITY \ SYSTEM.并且还禁用了诸如运行,启动,注释等"之类的属性.
我的问题是我想通过代码创建一个以用户管理员身份运行的计划作业.这是使用SYSTEM User创建计划的代码.如何与其他用户一起创建时间表?

Hi all

I am scheduling a task through code.By default the "Run as" user is
NT AUTHORITY\SYSTEM. And also the properties are disabled like "Run,Start in,Comment and so on".
My question is I want to create a scheduled job running as user administrator through code. Here is the code which creates the schedule with SYSTEM User. How can I create the schedule with other users?

string strJobId = "";
try
{
    int DaysOfMonth=0;
    int DaysOfWeek=0;
    ManagementClass classInstance = new ManagementClass("root\\CIMV2", "Win32_ScheduledJob", null);
    ManagementBaseObject inParams = classInstance.GetMethodParameters("Create");
    inParams["Command"] = "Notepad.exe";
    inParams["InteractWithDesktop"] = true;
    inParams["RunRepeatedly"] = true;
    if (DaysOfMonth > 0)
        inParams["DaysOfMonth"] = 0;
    if (DaysOfWeek > 0)
        inParams["DaysOfWeek"] = 0;
    inParams["StartTime"] = "20101129105409.000000+330";
    ManagementBaseObject outParams =
            classInstance.InvokeMethod("Create", inParams, null);

    strJobId = outParams["JobId"].ToString();
    Console.WriteLine("Out parameters:");
    Console.WriteLine("JobId: " + outParams["JobId"]);

    Console.WriteLine("ReturnValue: " + outParams["ReturnValue"]);
    }
    catch (ManagementException err)
    {

    }
    return strJobId;

推荐答案

Hello Froxy,

如果您正在寻找一个出色的图书馆来做您想要的事情.请通过 Dennis Austin (基于Dave Hall的作品)访问此项目:

http://www.codeproject.com/KB/cs/tsnewlib.aspx

我以前在某些项目中使用过它,并且非常喜欢它.使用适当的访问权限,您甚至可以使用它来访问远程计算机上的计划作业.


干杯

Manfred
Hello Froxy,

if you''re looking for a great library to do what you want. Please visit this project by Dennis Austin (based on work by Dave Hall):

http://www.codeproject.com/KB/cs/tsnewlib.aspx

I''ve used it in some projects before and like it very much. With the appropriate access rights you can even access scheduled jobs on remote computers with it.


Cheers

Manfred


我担心根本无法创建以其他用户身份运行的计划作业.请参见WMI类定义:

I fear that creating a scheduled job running running as another user is not possible at all. See the WMI class definition:

class Win32_ScheduledJob : CIM_Job
{
  string   Caption;
  string   Command;
  uint32   DaysOfMonth;
  uint32   DaysOfWeek;
  string   Description;
  datetime ElapsedTime;
  datetime InstallDate;
  boolean  InteractWithDesktop;
  uint32   JobId;
  string   JobStatus;
  string   Name;
  string   Notify;
  string   Owner;
  uint32   Priority;
  boolean  RunRepeatedly;
  datetime StartTime;
  string   Status;
  datetime TimeSubmitted;
  datetime UntilTime;
};



没有类似RunAs的参数,也不能指定密码.我看到完成此操作的唯一方法是使用TaskScheduler库.

另请阅读: http://msdn.microsoft.com/zh-CN /library/aa394399(v=VS.85).aspx

在该页面的第一句话中,您被告知WMI作业调度与AT命令的工作原理相同.使用AT命令也
无法指定用户帐户&密码.


干杯

曼弗雷德(Manfred)



There is no parameter like RunAs and neither a password can be specified. The only way I see this being done is using the TaskScheduler library.

Also read this: http://msdn.microsoft.com/en-us/library/aa394399(v=VS.85).aspx

In the very first sentence of that page you''re told that WMI job scheduling works the same as AT command. Using AT command it''s also
impossible to specify a user account & password.


Cheers

Manfred


这篇关于如何通过WMI在管理员用户中创建计划任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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