Click here to Skip to main content
16,018,264 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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?
C#
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;
Posted
Updated 29-Nov-10 20:48pm
v3
Comments
froxy 30-Nov-10 5:57am    
The code works well with other users also.Thank you very much.but in my project WMI has been used already.Do you know how to include administrator user to create a schedule in WMI?If i can do this with WMI it will be very useful.
Please it is urgent. cn u help in this regard.
thanks in advance.
Manfred Rudolf Bihy 30-Nov-10 6:47am    
Was this meant as a comment on my answer below? If you don't write a comment to the corresponding answer, the person who answered will not be notified. Only if you respond via a comment to my answer will I be able to receive the notification. Thank you!

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
 
Share this answer
 
Comments
Dalek Dave 30-Nov-10 6:59am    
Good Call.
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;
};


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
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900