Task Scheduler TaskRunLevel

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I am trying to create a scheduled task using Win32.TaskScheduler. I need to create it at the logon event (the code below is called at logon). The user who is creating the task (always just a domain user) has to have the rights to create Scheduled Tasks.
I have accomplished this as if I use the code below then the scheduled task is created (I have given rights on the Tasks folder to domain users).
As you can see from the code, I am creating the task under the rights assigned to the person currently logged in:
(<span style="text-decoration:underline userId = string.Concat(Environment.UserDomainName, "\", Environment.UserName);
The <span style="text-decoration:underline userid (currently logged on user) is what appears under the heading "when running the task use the following user account" when the task is created.
However, I need to run the task <span style="text-decoration:underline when the user is logged out . When the code below is run, the task is created with the box "run while user is logged out" ticked so I thought this would ensure
the task runs while the user is logged out <span style="text-decoration:underline
but it doesnt. I have discovered it will only run while the user is logged out
<span style="text-decoration:underline if the task created by the code below also has
<span style="text-decoration:underline the box "runs with highest priveleges" ticked
. No problem I thought;I just added the line <span style="text-decoration:underline tdall.Principal.RunLevel = TaskRunLevel.Highest .
However when I do this it gives me an access denied error in the catch block. This makes no sense because I can manually change the run level on the PC while logged on as the user the code is running under (by ticking the box "runs with
highest priveleges").
I tried it another way by removing the line that causes the error ( tdall.Principal.RunLevel = TaskRunLevel.Highest .) and also changing the line
ts.RootFolder.RegisterTaskDefinition(taskName, tdall, TaskCreation.CreateOrUpdate, userId, LogonType: TaskLogonType.S4U);
to
ts.RootFolder.RegisterTaskDefinition(taskName, tdall, TaskCreation.CreateOrUpdate, "SYSTEM", LogonType: TaskLogonType.S4U);
This is because entering " <span style="text-decoration:underline SYSTEM"
guarantees a task will run while logged out.
<span style="text-decoration:underline SYSTEM will appear under the heading "<span style="text-decoration:underline when running the task use the following user account".
However, when I try to do this, I also get access denied error in the catch block.

If I log on as Administrator I can do all of the above no problem. As I say, it makes no sense as the Domain user has the same rights as the Administrator on the
Tasks folder.
Any help would be appreciated, thanks
<br/>

<br/>

<pre class="prettyprint" style=" try
{
string userId = string.Concat(Environment.UserDomainName, "\", Environment.UserName);



TaskDefinition tdall = ts.NewTask();
userId = string.Concat(Environment.UserDomainName, "\", Environment.UserName);

tdall.RegistrationInfo.Description = schtask.ToString() + i.ToString();
tdall.Principal.LogonType = TaskLogonType.S4U;
tdall.Principal.RunLevel = TaskRunLevel.Highest;
tdall.Principal.UserId = userId;

DailyTrigger dtall = new DailyTrigger();

// Add a trigger that will fire the task at this time every day
dtall = (DailyTrigger)tdall.Triggers.Add(new DailyTrigger { DaysInterval = 1 });
dtall.StartBoundary = schtime;


tdall.Actions.Add(new ExecAction("psshutdown.exe", "-d -accepteula", @"C:EnergyWatchITApp_Files"));

i++;
string taskName = "EWITSchedSleepAll" + schtask.ToString() + i.ToString();



ts.RootFolder.RegisterTaskDefinition(taskName, tdall, TaskCreation.CreateOrUpdate, userId, LogonType: TaskLogonType.S4U);




}
catch (Exception j)
{
Logger.append("j is " + j.Message + Environment.NewLine + j.StackTrace, Logger.ALL);
MessageBox.Show("j is " + j.ToString());
}[/code]
<br/>
<br/>

<br/>

View the full article
 

Similar threads

Back
Top