Link to home
Start Free TrialLog in
Avatar of YZlat
YZlatFlag for United States of America

asked on

Scheduled task security context dilemma

I have a scheduled task that runs exe which is a windows application. In order to see UI, I need to check "run only if user is logged on", but when I create a scheduled task I cannot specify which user it will run under, so if I specify System account, the exe will run without user interface. User interface only shows up if I run the task as myself while I am logged on.

The thing is that the task will be created on multiple machines and will have to run in logged in user context and be interractive. How can I accomplish that?

the task is created programmatically, using C# and Microsoft.Win32.TaskScheduler namespace

Can anyone help??
Avatar of BuggyCoder
BuggyCoder
Flag of India image

Avatar of YZlat

ASKER

My question is more specific. And the example you posted does not require user interface, it runs on a background.

I already have all that code and it is fine running on  a background under System account. I need it to run under user account of currently logged in user
ASKER CERTIFIED SOLUTION
Avatar of David Johnson, CD
David Johnson, CD
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of YZlat

ASKER

what kind of account is  builtin\users??

Will it enable the scheduled task to run an exe with UI?
Avatar of YZlat

ASKER

I tried that group and got an error "This LogonType is not supported on Task Scheduler 1.0"
It is a group account All computer users are members of this group. Yes I tested it with Notepad and it ran fineNotepad-Test.xml
Avatar of YZlat

ASKER

Here is the code that gave me this error "This LogonType is not supported on Task Scheduler 1.0"


  using (TaskService ts = new TaskService())
                {
                    
                    TaskDefinition td = ts.NewTask();
                    td.RegistrationInfo.Description = "task that runs exe";
                    TimeTrigger tt = new TimeTrigger();
                    tt.Enabled = true;
                    tt.StartBoundary = DateTime.Now + TimeSpan.FromMinutes(5);
                    tt.Repetition.Interval = TimeSpan.FromHours(1);
                    td.Triggers.Add(tt);

                    td.Principal.RunLevel = TaskRunLevel.Highest;
                    td.Actions.Add(new ExecAction("MyService.exe", null, Environment.GetEnvironmentVariable("windir")));
                    ts.RootFolder.RegisterTaskDefinition("MyService", td, TaskCreation.Create, @"BUILTIN\Users", null, TaskLogonType.Group, null);

                    
                }

Open in new window

Avatar of YZlat

ASKER

I also tried to setup a scheduled task manually and use BUILTIN\Users group and got an error in EventViewer Security log:

Logon Failure:
       Reason:            Unknown user name or bad password
       User Name:      Users
       Domain:            BUILTIN
Avatar of YZlat

ASKER

I modified the task manually to run as BUILTIN\Users and it did not run on a scheduld time. I selected "Run only if logged on" checkbox
that dll doesn't have all the bases covered..
edit the .xml that I provided and then execute
schtasks /create /tn "notepadtest" /xml notepad-test.xml /f

the time is in UTC
Avatar of YZlat

ASKER

I  tried adding BUILTIN\Users to tak manually but it can only add 'ComputerName\Users' or 'DOMAINNAME\Domain users'

And I found that BUILTIN\Users only good for Windows 7 but does not work on Windows XP, so my problem still stays unsolved:(
Avatar of YZlat

ASKER

Do you have any other suggestions? You cam closer than anyone else so far

What dll were you talking about?
Avatar of YZlat

ASKER

Looks like BUILTIN\Users worked for Windows 7. But what do I do with Windows XP?
Avatar of YZlat

ASKER

Turns out I do not need it for windows xp, only for windows 7.

Thanks a lot!
Avatar of YZlat

ASKER

Thank you! You are the best!