Hi dahall
I have following 2003 server
Windows Server 2003, Standard Edition
Build 3790 (Service Pack 2)
Ajay
Hi dahall
I have following 2003 server
Windows Server 2003, Standard Edition
Build 3790 (Service Pack 2)
Ajay
I haven't tried your specific scenario, but the Scheduled Tasks applet uses the same base COM library as this library. I would expect that you will need to specify the username and logontype of S4U or InteractiveToken in the RegisterTaskDefinition method and ensure that your settings in code match the settings of those tasks created using the applet.
2008 can see tasks on other 2008 and 2003 servers. 2003 can only see other 2003.
This is the latest code I tried. I'm pretty sure I'm just missing something small. Your help is greatly appreciated:
var taskDef = ts.NewTask(); if (ts.HighestSupportedVersion >= new Version(1, 2)) { taskDef.Principal.RunLevel = TaskRunLevel.LUA; } else { taskDef.Settings.RunOnlyIfLoggedOn = true; } string userId = string.Concat(Environment.UserDomainName, "\\", Environment.UserName); taskDef.Principal.LogonType = TaskLogonType.S4U; taskDef.Principal.UserId = userId; taskDef.RegistrationInfo.Description = string.Format( Resources.Scheduled_Task_Desc_Format, this.TargetEntity.FriendlyName ); taskDef.Triggers.Add(GetTrigger(_syncSettingsViewModel.Interval)); taskDef.Actions.Add( new ExecAction( Assembly.GetEntryAssembly().Location, "-s " + GetSyncFlowName(this.TargetEntity) ) ); ts.RootFolder.RegisterTaskDefinition( taskName, taskDef, TaskCreation.CreateOrUpdate, userId, LogonType: TaskLogonType.S4U );
Great news! I found the solution based on the following thread:
Based on the two recommendations,
I'm not sure how critical #1 is, however #2 will get you past this exception. Creates and Deletes all work now :)
For everyone's benefit, here is the simplified code to make this work for non-admin users on XP and Win7
var taskDef = ts.NewTask(); taskDef.Settings.ExecutionTimeLimit = TimeSpan.FromMinutes(15); taskDef.RegistrationInfo.Description = "My Task"; taskDef.Actions.Add(new ExecAction( Assembly.GetEntryAssembly().Location, "-myArgs" ) ); string userId = string.Concat(Environment.UserDomainName, "\\", Environment.UserName); TaskLogonType logonType; if (ts.HighestSupportedVersion >= _v2) { taskDef.Principal.RunLevel = TaskRunLevel.LUA; taskDef.Principal.LogonType = logonType = TaskLogonType.S4U; taskDef.Principal.UserId = userId;
taskDef.Triggers.Add(GetTrigger(interval));
} else // For Windows XP { taskDef.Settings.RunOnlyIfLoggedOn = true; logonType = TaskLogonType.InteractiveToken; // Avoid System.ArgumentException: // Trigger.Repetition.Interval must be less than // Trigger.Repetition.Duration under Task Scheduler 1.0 var trigger = GetTrigger(interval); trigger.Repetition.Duration = trigger.Repetition.Interval.Add(TimeSpan.FromMinutes(1)); trigger.Repetition.StopAtDurationEnd = false; taskDef.Triggers.Add(trigger); } var taskFolder = GetTaskFolder(ts); taskFolder.RegisterTaskDefinition( taskName, taskDef, TaskCreation.CreateOrUpdate, userId, LogonType: logonType ); // Add this helper method private TaskFolder GetCeligoTaskFolder(TaskService ts) { if (ts.HighestSupportedVersion >= _v2) { return ts.RootFolder .SubFolders .FirstOrDefault(f => f.Name == "MyTaskFolder") ?? ts.RootFolder.CreateFolder("MyTaskFolder"); } else { return ts.RootFolder; } }
We have tasks listed in a folder on our Windows Server 2008 R2, and can loop through tasks, however, items aren't showing correct information.
NextRunTime; // it's showing 12/30/1899 12:00:00 AM Status // it's showing 'Ready' even while its 'Running' on Task Scheduler
Our setup:
Task Scheduler > Reports > DailyExcelReport1
Task Scheduler > Reports > DailyExcelReport2
Task Scheduler > Reports > DailyExcelReport3
and so on...
To access it via code:
var State = ""; var NextRunTime; using (var taskService = new TaskService("servername", "username", "DOMAIN", "password1")) { if (taskService.Connected) { var tasks = taskService.GetFolder("Reports").Tasks;� foreach (var task in tasks) { State = task.State.ToString(); // it's showing 'Ready' even while its 'Running' on Task Scheduler NextRunTime = task.NextRunTime; // it's showing 12/30/1899 12:00:00 AM } } }
Hi All please guide me how to get task from remote server
Hi all,
I have a task which created manually. Each time it runs, I need to check some conditions from my Database to determine its NextRunTime but NexRunTime property is readonly.
How can I change the NextRunTime value or is there anyway to define its next run time ?
Thanks for your help.
NextRunTime is calculated internally by looking at all the triggers and determining when they will all execute. The only way to change it is by changing some or all of the triggers associated with the task.
bump
Hi ALL
I Want to call remotely windows task scheduler for this i am using Microsoft.Win32.TaskScheduler.dll and created object of TaskService class like
TaskService 01_server = getTaskService("\\\\123.123.222.222", "administrator", "domain1", "pwd0", false);
TaskService 02_server = getTaskService("\\\\123.122.222.222", "administrator", "domain2", "pwd1", false);
and bind task to grodview
for server 01_serve
DataTable dataTable = new DataTable();
dataTable.Columns.Add("TaskName");
dataTable.Columns.Add("TaskSchedule");
dataTable.Columns.Add("TaskNextRun");
dataTable.Columns.Add("TaskLastRun");
dataTable.Columns.Add("State");
dataTable.AcceptChanges();
DataRow dataRow;
StringBuilder sbTaskSchedule = new StringBuilder();
Version ver = 01_server .HighestSupportedVersion;
bool newVer = (ver >= new Version(1, 2));
TaskFolder tf = 01_serve.RootFolder;
int ctaskNx =01_serve.RootFolder.Tasks.Count;
for (int i = 0; i < ctaskNx; i++)
{
m_Task = 01_serve.RootFolder.Tasks[i];
string strFileNamePath = m_Task.Definition.Actions.ToString().Trim();
if (strFileNamePath.Contains(".vbs"))
{
dataRow = dataTable.NewRow();
dataRow["TaskName"] = m_Task.Name.ToString().Trim();
dataRow["TaskNextRun"] = m_Task.NextRunTime.ToString().Trim();
dataRow["TaskLastRun"] = m_Task.LastRunTime.ToString().Trim();
dataRow["State"] = m_Task.State.ToString().Trim();
dataTable.Rows.Add(dataRow);
dataTable.AcceptChanges();
}
}
for server 02_serve
Version ver = 02_server .HighestSupportedVersion;
bool newVer = (ver >= new Version(1, 2));
TaskFolder tf = 02_server.RootFolder;
int ctaskNx = 02_server.RootFolder.Tasks.Count;
for (int i = 0; i < ctaskNx; i++)
{
m_Task = 02_server.RootFolder.Tasks[i];
string strFileNamePath = m_Task.Definition.Actions.ToString().Trim();
if (strFileNamePath.Contains(".vbs"))
{
dataRow = dataTable.NewRow();
dataRow["TaskName"] = m_Task.Name.ToString().Trim();
dataRow["TaskNextRun"] = m_Task.NextRunTime.ToString().Trim();
dataRow["TaskLastRun"] = m_Task.LastRunTime.ToString().Trim();
dataRow["State"] = m_Task.State.ToString().Trim();
dataTable.Rows.Add(dataRow);
dataTable.AcceptChanges();
}
}
but i am not able to get task from remote server
please guide me where i am wrong
thanks
vinod
Hi David,
I have a couple of questions for you:
(1) I noticed that if I choose "Configure for: Windows Server 2003, Windows XP, or Windows 2000", save the task schedule, and reopen or edit it, the "Configure for:" value doesn't stick to this chosen OS when the task schedule was saved. Is this by design? ...meaning it defaults to whatever is your current OS.
(2) Can we add Windows 8 to the list...together with Windows 7, Vista, and Windows Server 2008 in the Configure for: <OS> choices?
Thanks, Audi
On your first item, you found a bug. I have corrected the problem in the code and will release it with the next update (likely 1.8.4).
On your second point, I haven't done this yet as Windows 8 has not released and I don't know yet if they will be enhancing the base COM library or what the final UI will look like on the RTM Windows 8 Task Scheduler MMC. Once I know both those things, I will update the library.
Found the problem... I had assigned permissions to C:\Tasks, but on this machine the Tasks are stored at C:\Windows\System32\Tasks -- once I assigned the folder the correct permissions, then it worked. Sorry for the wild goose chase.
Fixed
Fixed. Cause is mixing old .NET with new (>3.5) .NET assemblies.