Task Scheduler Changes

- added on Login option to Auto-Start server.
This commit is contained in:
Brett Hewitson 2022-08-08 00:39:22 +10:00
parent 795055f2f1
commit 19a6a80c56
7 changed files with 92 additions and 26 deletions

View file

@ -1301,8 +1301,12 @@
<sys:String x:Key="ServerSettings_AutomaticManagementLabel">Automatic Management</sys:String>
<sys:String x:Key="ServerSettings_NoAutoManagementButtonLabel">Why can't I use this?</sys:String>
<sys:String x:Key="ServerSettings_ASMSettingLabel">Server Manager Settings</sys:String>
<sys:String x:Key="ServerSettings_AutoStartLabel">Auto-Start server on boot</sys:String>
<sys:String x:Key="ServerSettings_AutoStartTooltip">Automatically start the server when the machine starts.</sys:String>
<sys:String x:Key="ServerSettings_AutoStartServerLabel">Auto-Start server</sys:String>
<sys:String x:Key="ServerSettings_AutoStartServerTooltip">Automatically start the server.</sys:String>
<sys:String x:Key="ServerSettings_AutoStartServerOnBootLabel">on Boot</sys:String>
<sys:String x:Key="ServerSettings_AutoStartServerOnBootTooltip">Start the server when the machine starts.</sys:String>
<sys:String x:Key="ServerSettings_AutoStartServerOnLoginLabel">on Login</sys:String>
<sys:String x:Key="ServerSettings_AutoStartServerOnLoginTooltip">Start the server when the user logs in.</sys:String>
<sys:String x:Key="ServerSettings_EnableAutoShutdownLabel">Shutdown server at</sys:String>
<sys:String x:Key="ServerSettings_EnableAutoShutdownTooltip">The hour and minute of the day to force a server shutdown.</sys:String>
<sys:String x:Key="ServerSettings_RestartAfterShutdownLabel">then restart</sys:String>

View file

@ -870,6 +870,14 @@ namespace ServerManagerTool.Lib
set { SetValue(EnableAutoStartProperty, value); }
}
public static readonly DependencyProperty AutoStartOnLoginProperty = DependencyProperty.Register(nameof(AutoStartOnLogin), typeof(bool), typeof(ServerProfile), new PropertyMetadata(false));
[DataMember]
public bool AutoStartOnLogin
{
get { return (bool)GetValue(AutoStartOnLoginProperty); }
set { SetValue(AutoStartOnLoginProperty, value); }
}
public static readonly DependencyProperty EnableAutoUpdateProperty = DependencyProperty.Register(nameof(EnableAutoUpdate), typeof(bool), typeof(ServerProfile), new PropertyMetadata(false));
[DataMember]
public bool EnableAutoUpdate
@ -4603,13 +4611,14 @@ namespace ServerManagerTool.Lib
var taskKey = GetProfileKey();
if(!TaskSchedulerUtils.ScheduleAutoStart(taskKey, null, this.EnableAutoStart, GetLauncherFile(), ProfileName, true, Config.Default.AutoStart_TaskPriority))
if (!TaskSchedulerUtils.ScheduleAutoStart(taskKey, null, this.EnableAutoStart, GetLauncherFile(), ProfileName, this.AutoStartOnLogin, Config.Default.AutoStart_TaskPriority))
{
return false;
}
TimeSpan shutdownTime;
var command = Assembly.GetEntryAssembly().Location;
if (!TaskSchedulerUtils.ScheduleAutoShutdown(taskKey, "#1", command, this.EnableAutoShutdown1 ? (TimeSpan.TryParseExact(this.AutoShutdownTime1, "g", null, out TimeSpan shutdownTime) ? shutdownTime : (TimeSpan?)null) : null, ShutdownDaysOfTheWeek1, ProfileName, TaskSchedulerUtils.ShutdownType.Shutdown1, Config.Default.AutoShutdown_TaskPriority))
if (!TaskSchedulerUtils.ScheduleAutoShutdown(taskKey, "#1", command, this.EnableAutoShutdown1 ? (TimeSpan.TryParseExact(this.AutoShutdownTime1, "g", null, out shutdownTime) ? shutdownTime : (TimeSpan?)null) : null, ShutdownDaysOfTheWeek1, ProfileName, TaskSchedulerUtils.ShutdownType.Shutdown1, Config.Default.AutoShutdown_TaskPriority))
{
return false;
}

View file

@ -1400,7 +1400,12 @@
</Style>
</Grid.Style>
<cctl:CheckBoxAndTextBlock Grid.Row="0" Margin="5,5,5,0" Text="{DynamicResource ServerSettings_AutoStartLabel}" IsChecked="{Binding EnableAutoStart, Mode=TwoWay}" VerticalAlignment="Center" ToolTip="{DynamicResource ServerSettings_AutoStartTooltip}" HorizontalAlignment="Left"/>
<StackPanel Grid.Row="0" Orientation="Horizontal" Margin="5,5,5,0">
<cctl:CheckBoxAndTextBlock Text="{DynamicResource ServerSettings_AutoStartServerLabel}" VerticalAlignment="Bottom" IsChecked="{Binding EnableAutoStart, Mode=TwoWay}" ToolTip="{DynamicResource ServerSettings_AutoStartServerTooltip}" HorizontalAlignment="Left"/>
<RadioButton GroupName="AutoStartServer" Margin="10,0,0,0" VerticalAlignment="Bottom" IsEnabled="{Binding EnableAutoStart}" IsChecked="{Binding AutoStartOnLogin, Converter={StaticResource InvertBooleanConverter}}" Content="{DynamicResource ServerSettings_AutoStartServerOnBootLabel}" ToolTip="{DynamicResource ServerSettings_AutoStartServerOnBootTooltip}"/>
<RadioButton GroupName="AutoStartServer" Margin="5,0,0,0" VerticalAlignment="Bottom" IsEnabled="{Binding EnableAutoStart}" IsChecked="{Binding AutoStartOnLogin}" Content="{DynamicResource ServerSettings_AutoStartServerOnLoginLabel}" ToolTip="{DynamicResource ServerSettings_AutoStartServerOnLoginTooltip}"/>
</StackPanel>
<StackPanel Grid.Row="1" Orientation="Horizontal" Margin="5,5,5,0" IsEnabled="{Binding ElementName=EnableSOTFCheckbox, Path=IsChecked, Converter={StaticResource InvertBooleanConverter}}" >
<cctl:CheckBoxAndTextBlock Name="EnableAutoShutdown1Checkbox" IsChecked="{Binding EnableAutoShutdown1, Mode=TwoWay}" Text="{DynamicResource ServerSettings_EnableAutoShutdownLabel}" VerticalAlignment="Bottom" ToolTip="{DynamicResource ServerSettings_EnableAutoShutdownTooltip}"/>

View file

@ -1237,8 +1237,12 @@
<sys:String x:Key="ServerSettings_AutomaticManagementLabel">Automatic Management</sys:String>
<sys:String x:Key="ServerSettings_NoAutoManagementButtonLabel">Why can't I use this?</sys:String>
<sys:String x:Key="ServerSettings_ServerManagerSettingsLabel">Server Manager Settings</sys:String>
<sys:String x:Key="ServerSettings_AutoStartLabel">Auto-Start server on boot</sys:String>
<sys:String x:Key="ServerSettings_AutoStartTooltip">Automatically start the server when the machine starts.</sys:String>
<sys:String x:Key="ServerSettings_AutoStartServerLabel">Auto-Start server</sys:String>
<sys:String x:Key="ServerSettings_AutoStartServerTooltip">Automatically start the server.</sys:String>
<sys:String x:Key="ServerSettings_AutoStartServerOnBootLabel">on Boot</sys:String>
<sys:String x:Key="ServerSettings_AutoStartServerOnBootTooltip">Start the server when the machine starts.</sys:String>
<sys:String x:Key="ServerSettings_AutoStartServerOnLoginLabel">on Login</sys:String>
<sys:String x:Key="ServerSettings_AutoStartServerOnLoginTooltip">Start the server when the user logs in.</sys:String>
<sys:String x:Key="ServerSettings_EnableAutoShutdownLabel">Shutdown server at</sys:String>
<sys:String x:Key="ServerSettings_EnableAutoShutdownTooltip">The hour and minute of the day to force a server shutdown.</sys:String>
<sys:String x:Key="ServerSettings_RestartAfterShutdownLabel">then restart</sys:String>

View file

@ -486,6 +486,14 @@ namespace ServerManagerTool.Lib
set { SetValue(EnableAutoStartProperty, value); }
}
public static readonly DependencyProperty AutoStartOnLoginProperty = DependencyProperty.Register(nameof(AutoStartOnLogin), typeof(bool), typeof(ServerProfile), new PropertyMetadata(false));
[DataMember]
public bool AutoStartOnLogin
{
get { return (bool)GetValue(AutoStartOnLoginProperty); }
set { SetValue(AutoStartOnLoginProperty, value); }
}
public static readonly DependencyProperty EnableAutoUpdateProperty = DependencyProperty.Register(nameof(EnableAutoUpdate), typeof(bool), typeof(ServerProfile), new PropertyMetadata(false));
[DataMember]
public bool EnableAutoUpdate
@ -1240,7 +1248,7 @@ namespace ServerManagerTool.Lib
var taskKey = GetProfileKey();
if(!TaskSchedulerUtils.ScheduleAutoStart(taskKey, null, this.EnableAutoStart, GetLauncherFile(), ProfileName, true, Config.Default.AutoStart_TaskPriority))
if (!TaskSchedulerUtils.ScheduleAutoStart(taskKey, null, this.EnableAutoStart, GetLauncherFile(), ProfileName, this.AutoStartOnLogin, Config.Default.AutoStart_TaskPriority))
{
return false;
}

View file

@ -981,7 +981,11 @@
</Style>
</Grid.Style>
<CheckBox Grid.Row="0" Margin="5,5,5,0" Content="{DynamicResource ServerSettings_AutoStartLabel}" IsChecked="{Binding EnableAutoStart}" VerticalAlignment="Center" ToolTip="{DynamicResource ServerSettings_AutoStartTooltip}" HorizontalAlignment="Left"/>
<StackPanel Grid.Row="0" Orientation="Horizontal" Margin="5,5,5,0">
<CheckBox Content="{DynamicResource ServerSettings_AutoStartServerLabel}" VerticalAlignment="Bottom" IsChecked="{Binding EnableAutoStart}" ToolTip="{DynamicResource ServerSettings_AutoStartServerTooltip}" HorizontalAlignment="Left"/>
<RadioButton GroupName="AutoStartServer" Margin="10,0,0,0" VerticalAlignment="Bottom" IsEnabled="{Binding EnableAutoStart}" IsChecked="{Binding AutoStartOnLogin, Converter={StaticResource InvertBooleanConverter}}" Content="{DynamicResource ServerSettings_AutoStartServerOnBootLabel}" ToolTip="{DynamicResource ServerSettings_AutoStartServerOnBootTooltip}"/>
<RadioButton GroupName="AutoStartServer" Margin="5,0,0,0" VerticalAlignment="Bottom" IsEnabled="{Binding EnableAutoStart}" IsChecked="{Binding AutoStartOnLogin}" Content="{DynamicResource ServerSettings_AutoStartServerOnLoginLabel}" ToolTip="{DynamicResource ServerSettings_AutoStartServerOnLoginTooltip}"/>
</StackPanel>
<StackPanel Grid.Row="1" Orientation="Horizontal" Margin="5,5,5,0" >
<CheckBox Name="EnableAutoShutdown1Checkbox" IsChecked="{Binding EnableAutoShutdown1}" Content="{DynamicResource ServerSettings_EnableAutoShutdownLabel}" VerticalAlignment="Bottom" ToolTip="{DynamicResource ServerSettings_EnableAutoShutdownTooltip}"/>

View file

@ -185,6 +185,7 @@ namespace ServerManagerTool.Common.Utils
Task task = null;
TaskDefinition taskDefinition = null;
TaskLogonType taskLogonType = TaskLogonType.InteractiveToken;
try
{
@ -204,8 +205,9 @@ namespace ServerManagerTool.Common.Utils
Version.TryParse(AppUtils.GetDeployedVersion(), out Version appVersion);
taskDefinition.Principal.LogonType = TaskLogonType.InteractiveToken;
taskDefinition.Principal.LogonType = taskLogonType;
taskDefinition.Principal.RunLevel = TaskRunLevel.Highest;
taskDefinition.Principal.UserId = null;
taskDefinition.RegistrationInfo.Description = "Server Auto-Backup";
taskDefinition.RegistrationInfo.Source = "Server Manager";
@ -245,7 +247,7 @@ namespace ServerManagerTool.Common.Utils
try
{
task = taskFolder.RegisterTaskDefinition(taskName, taskDefinition, TaskCreation.CreateOrUpdate, null, null, TaskLogonType.InteractiveToken);
task = taskFolder.RegisterTaskDefinition(taskName, taskDefinition, TaskCreation.CreateOrUpdate, null, null, taskLogonType);
return task != null;
}
catch (Exception ex)
@ -309,6 +311,7 @@ namespace ServerManagerTool.Common.Utils
Task task = null;
TaskDefinition taskDefinition = null;
TaskLogonType taskLogonType = TaskLogonType.InteractiveToken;
try
{
@ -328,8 +331,9 @@ namespace ServerManagerTool.Common.Utils
Version.TryParse(AppUtils.GetDeployedVersion(), out Version appVersion);
taskDefinition.Principal.LogonType = TaskLogonType.InteractiveToken;
taskDefinition.Principal.LogonType = taskLogonType;
taskDefinition.Principal.RunLevel = TaskRunLevel.Highest;
taskDefinition.Principal.UserId = null;
taskDefinition.RegistrationInfo.Description = $"Server Auto-Shutdown - {profileName}";
taskDefinition.RegistrationInfo.Source = "Server Manager";
@ -393,7 +397,7 @@ namespace ServerManagerTool.Common.Utils
try
{
task = taskFolder.RegisterTaskDefinition(taskName, taskDefinition, TaskCreation.CreateOrUpdate, null, null, TaskLogonType.InteractiveToken);
task = taskFolder.RegisterTaskDefinition(taskName, taskDefinition, TaskCreation.CreateOrUpdate, null, null, taskLogonType);
return task != null;
}
catch (Exception ex)
@ -426,7 +430,7 @@ namespace ServerManagerTool.Common.Utils
return false;
}
public static bool ScheduleAutoStart(string taskKey, string taskSuffix, bool enableAutoStart, string command, string profileName, bool onBoot, ProcessPriorityClass priority)
public static bool ScheduleAutoStart(string taskKey, string taskSuffix, bool enableAutoStart, string command, string profileName, bool onLogin, ProcessPriorityClass priority)
{
var taskName = GetScheduledTaskName(TaskType.AutoStart, taskKey, taskSuffix);
var taskFolder = TaskService.Instance.RootFolder.SubFolders.Exists(TaskFolder) ? TaskService.Instance.RootFolder.SubFolders[TaskFolder] : null;
@ -452,6 +456,8 @@ namespace ServerManagerTool.Common.Utils
Task task = null;
TaskDefinition taskDefinition = null;
TaskLogonType taskLogonType;
string userId;
try
{
@ -471,7 +477,6 @@ namespace ServerManagerTool.Common.Utils
Version.TryParse(AppUtils.GetDeployedVersion(), out Version appVersion);
taskDefinition.Principal.LogonType = TaskLogonType.ServiceAccount;
taskDefinition.Principal.RunLevel = TaskRunLevel.Highest;
taskDefinition.RegistrationInfo.Description = $"Server Auto-Start - {profileName}";
@ -481,16 +486,28 @@ namespace ServerManagerTool.Common.Utils
taskDefinition.Settings.ExecutionTimeLimit = TimeSpan.FromHours(EXECUTION_TIME_LIMIT);
taskDefinition.Settings.Priority = priority;
// Add a trigger that will fire after the machine has started
if (onBoot)
if (onLogin)
{
var triggers = taskDefinition.Triggers.OfType<BootTrigger>();
taskLogonType = TaskLogonType.InteractiveToken;
taskDefinition.Principal.LogonType = taskLogonType;
taskDefinition.Principal.UserId = null;
userId = null;
var oldtriggers = taskDefinition.Triggers.OfType<BootTrigger>();
foreach (var trigger in oldtriggers)
{
taskDefinition.Triggers.Remove(trigger);
}
// Add a trigger that will fire after a user has logged in
var triggers = taskDefinition.Triggers.OfType<LogonTrigger>();
if (triggers.IsEmpty())
{
var trigger = new BootTrigger
var trigger = new LogonTrigger
{
Delay = TimeSpan.FromMinutes(1),
ExecutionTimeLimit = TimeSpan.FromHours(EXECUTION_TIME_LIMIT)
ExecutionTimeLimit = TimeSpan.FromHours(EXECUTION_TIME_LIMIT),
UserId = System.Security.Principal.WindowsIdentity.GetCurrent().Name,
};
taskDefinition.Triggers.Add(trigger);
}
@ -499,18 +516,31 @@ namespace ServerManagerTool.Common.Utils
foreach (var trigger in triggers)
{
trigger.Delay = TimeSpan.FromMinutes(1);
trigger.UserId = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
}
}
}
else
{
var triggers = taskDefinition.Triggers.OfType<LogonTrigger>();
taskLogonType = TaskLogonType.ServiceAccount;
taskDefinition.Principal.LogonType = taskLogonType;
taskDefinition.Principal.UserId = null;
userId = "SYSTEM";
var oldtriggers = taskDefinition.Triggers.OfType<LogonTrigger>();
foreach (var trigger in oldtriggers)
{
taskDefinition.Triggers.Remove(trigger);
}
// Add a trigger that will fire after the machine has booted
var triggers = taskDefinition.Triggers.OfType<BootTrigger>();
if (triggers.IsEmpty())
{
var trigger = new LogonTrigger
var trigger = new BootTrigger
{
Delay = TimeSpan.FromMinutes(1),
ExecutionTimeLimit = TimeSpan.FromHours(EXECUTION_TIME_LIMIT)
ExecutionTimeLimit = TimeSpan.FromHours(EXECUTION_TIME_LIMIT),
};
taskDefinition.Triggers.Add(trigger);
}
@ -534,7 +564,7 @@ namespace ServerManagerTool.Common.Utils
try
{
task = taskFolder.RegisterTaskDefinition(taskName, taskDefinition, TaskCreation.CreateOrUpdate, "SYSTEM", null, TaskLogonType.ServiceAccount);
task = taskFolder.RegisterTaskDefinition(taskName, taskDefinition, TaskCreation.CreateOrUpdate, userId, null, taskLogonType);
return task != null;
}
catch (Exception ex)
@ -594,6 +624,7 @@ namespace ServerManagerTool.Common.Utils
Task task = null;
TaskDefinition taskDefinition = null;
TaskLogonType taskLogonType = TaskLogonType.InteractiveToken;
try
{
@ -613,8 +644,9 @@ namespace ServerManagerTool.Common.Utils
Version.TryParse(AppUtils.GetDeployedVersion(), out Version appVersion);
taskDefinition.Principal.LogonType = TaskLogonType.InteractiveToken;
taskDefinition.Principal.LogonType = taskLogonType;
taskDefinition.Principal.RunLevel = TaskRunLevel.Highest;
taskDefinition.Principal.UserId = null;
taskDefinition.RegistrationInfo.Description = "Server Auto-Update";
taskDefinition.RegistrationInfo.Source = "Server Manager";
@ -654,7 +686,7 @@ namespace ServerManagerTool.Common.Utils
try
{
task = taskFolder.RegisterTaskDefinition(taskName, taskDefinition, TaskCreation.CreateOrUpdate, null, null, TaskLogonType.InteractiveToken);
task = taskFolder.RegisterTaskDefinition(taskName, taskDefinition, TaskCreation.CreateOrUpdate, null, null, taskLogonType);
return task != null;
}
catch (Exception ex)