This project has moved. For the latest updates, please go here.

How do I store a password when adding task?

Aug 11, 2011 at 4:55 PM
Edited Aug 11, 2011 at 4:55 PM

I'm working to create a task on Windows 2008, and would like for the password to be stored when my task is added (the "Do not store password. The task will only have access to local resources" box will not be checked when viewed in Task Scheduler).

When using the schtasks.exe command line utility, this is accomplished by omitting the /NP parameter (I think).

In looking through the documentation and this discussion board, I don't see any way to control this...unless I just missed it. It seems that using TaskLogonType.Password would accomplish this, but that isn't working for me.

I'm using a variation of the provided example code:

 // Get the service on the local machine
            using (TaskService ts = new TaskService())
            {
                // Create a new task definition and assign properties
                TaskDefinition td = ts.NewTask();
                td.RegistrationInfo.Description = "My Task Description";
                td.Principal.LogonType = TaskLogonType.Password;
                td.Principal.RunLevel = TaskRunLevel.Highest;
               
                // Add a trigger that will fire every Sunday at 13:00
                td.Triggers.Add(new WeeklyTrigger { StartBoundary = DateTime.Today + TimeSpan.FromHours(13), DaysOfWeek = DaysOfTheWeek.Sunday });

                // Add an action that will launch MyApp.exe whenever the trigger fires
                td.Actions.Add(new ExecAction("MyApp.exe", "", null));

                // Register the task in the root folder
                const string taskName = "My task description";
                ts.RootFolder.RegisterTaskDefinition(taskName, td, TaskCreation.CreateOrUpdate, "someUserId", "somePassword");
            }

 Thanks in advance for any guidance on this.

 

 

 

Aug 11, 2011 at 5:47 PM

Just needed to dig a little more. Found that if I change

 ts.RootFolder.RegisterTaskDefinition(taskName, td, TaskCreation.CreateOrUpdate, "someUserId", "somePassword"); to be

 ts.RootFolder.RegisterTaskDefinition(taskName, td, TaskCreation.CreateOrUpdate, "someUserId", "somePassword", TaskLogonType.Password);

it works fine.



Marked as answer by dahall on 1/27/2017 at 11:19 AM
Jan 24 at 12:05 PM
Edited Jan 24 at 12:09 PM
hello

I am developing a wpf application on custom task scheduler. Application will not run without admin privileges or manifest file as it is the required. So is it possible to create task from my application without giving admin privileges on launch time.

I tried the above one and googled but error is getting into it.
here is my code that, i am using

using (var taskService = new TaskService())
            {
                // Create a new task definition and assign properties
                var taskDefinition = taskService.NewTask();
                taskDefinition.RegistrationInfo.Description = taskDescription;
                taskDefinition.RegistrationInfo.Author = WindowsIdentity.GetCurrent().Name;
                taskDefinition.Principal.UserId = WindowsIdentity.GetCurrent().Name;
                taskDefinition.Principal.RunLevel = TaskRunLevel.Highest;
                taskDefinition.Settings.DisallowStartIfOnBatteries = false;
                foreach (var trigger in triggers)
                {
                    taskDefinition.Triggers.Add(trigger._Trigger);
                }
                foreach (var action in actionList)
                {
                    taskDefinition.Actions.Add(action);
                }


                var spectraFolder = taskService.RootFolder.SubFolders.Where(sf => sf.Name.Equals(_taskFolderPath));
                var spectraTaskFolder = spectraFolder.FirstOrDefault() ??
                                        taskService.RootFolder.CreateFolder(_taskFolderPath);

                // Register the task in the Spectra Logic folder
                spectraTaskFolder.RegisterTaskDefinition(taskName, taskDefinition ,TaskCreation.CreateOrUpdate, taskDefinition.Principal.UserId, "12345678", TaskLogonType.Password);
            }
it return exception as null

Thanks in advance for any guidance

shubham jain
Coordinator
Jan 26 at 1:53 PM
Yes, Use the parameters to the TaskService constructor to supply an account and password that have Admin rights. See the Documentation for detail.
Marked as answer by dahall on 1/27/2017 at 11:18 AM