-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScheduledTaskHelper.cs
More file actions
427 lines (387 loc) · 16.2 KB
/
Copy pathScheduledTaskHelper.cs
File metadata and controls
427 lines (387 loc) · 16.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Security.Principal;
using System.Text;
using System.Threading.Tasks;
namespace LaunchThenClose
{
/*
[ADDING]
ScheduledTaskHelper.CreateLogonTask(
taskName: "LaunchThenClose AutoStart",
exePath: @"D:\source\repos\LaunchThenClose\bin\debug\LaunchThenClose.exe",
arguments: "--logging",
runAsSystem: true);
[REMOVING]
ScheduledTaskHelper.DeleteTask("LaunchThenClose AutoStart");
[NOTE]
SchTasks.exe does NOT expose most of the advanced Task Scheduler settings, including:
- AllowStartOnDemand
- StartWhenAvailable
- DisallowStartIfOnBatteries
- StopIfGoingOnBatteries
- MultipleInstancesPolicy
- ExecutionTimeLimit
- IdleSettings
- WakeToRun
- Hidden
- RunOnlyIfNetworkAvailable
- Priority
It can create basic tasks, but not fully configured tasks.
You could import a fully featured XML task definition using:
- schtasks /create /tn "LaunchThenClose AutoStart" /xml LaunchThenCloseTask.xml /f
[EXAMPLE XML]
<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.4" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
<RegistrationInfo>
<Date>2025-01-01T00:00:00</Date>
<Author>USERNAME</Author>
<Description>Auto-start elevated console app at logon</Description>
</RegistrationInfo>
<Triggers>
<LogonTrigger>
<Enabled>true</Enabled>
</LogonTrigger>
</Triggers>
<Principals>
<Principal id="Author">
<UserId>S-1-5-18</UserId> <!-- SYSTEM account -->
<RunLevel>HighestAvailable</RunLevel>
</Principal>
</Principals>
<Settings>
<MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
<DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
<StopIfGoingOnBatteries>false</StopIfGoingOnBatteries>
<AllowHardTerminate>true</AllowHardTerminate>
<StartWhenAvailable>true</StartWhenAvailable>
<RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
<IdleSettings>
<StopOnIdleEnd>false</StopOnIdleEnd>
<RestartOnIdle>false</RestartOnIdle>
</IdleSettings>
<AllowStartOnDemand>true</AllowStartOnDemand>
<Enabled>true</Enabled>
<Hidden>false</Hidden>
<RunOnlyIfIdle>false</RunOnlyIfIdle>
<WakeToRun>false</WakeToRun>
<ExecutionTimeLimit>PT0S</ExecutionTimeLimit>
<Priority>7</Priority>
</Settings>
<Actions Context="Author">
<Exec>
<Command>C:\Path\To\YourApp.exe</Command>
<WorkingDirectory>C:\Path\To</WorkingDirectory>
</Exec>
</Actions>
</Task>
*/
public static class ScheduledTaskHelper
{
/// <summary>
/// Creates an elevated scheduled task that runs at user logon.
/// </summary>
/// <param name="taskName">Name of the scheduled task.</param>
/// <param name="exePath">Full path to the executable.</param>
/// <param name="arguments">Optional command-line arguments.</param>
/// <param name="runAsSystem">If true, runs as SYSTEM. Otherwise runs as current user.</param>
public static void CreateLogonTask(
string taskName,
string exePath,
string arguments = "",
bool runAsSystem = true)
{
if (string.IsNullOrWhiteSpace(taskName))
throw new ArgumentException("Task name cannot be empty.", nameof(taskName));
if (string.IsNullOrWhiteSpace(exePath))
throw new ArgumentException("Executable path cannot be empty.", nameof(exePath));
// Build the command line
var sb = new StringBuilder();
sb.Append($"/create /tn \"{taskName}\" ");
sb.Append("/sc onlogon ");
if (string.IsNullOrEmpty(arguments))
sb.Append($"/tr \"\\\"{exePath}\\\"");
else
sb.Append($"/tr \"\\\"{exePath}\\\" {arguments}\" ");
sb.Append("/rl highest ");
if (runAsSystem)
{
sb.Append("/ru SYSTEM ");
}
else
{
sb.Append($"/ru \"{Environment.UserName}\" ");
}
/*
If you need to run the task as a specific admin account:
C:\>schtasks /create /ru AdminUser /rp AdminPassword ...
*/
sb.Append("/f");
RunSchtasks(sb.ToString());
}
/// <summary>
/// Deletes the scheduled task.
/// </summary>
/// <param name="taskName">Name of the scheduled task.</param>
public static void DeleteTask(string taskName)
{
if (string.IsNullOrWhiteSpace(taskName))
throw new ArgumentException("Task name cannot be empty.", nameof(taskName));
RunSchtasks($"/delete /tn \"{taskName}\" /f");
}
/// <summary>
/// Determines whether a scheduled task with the specified name exists on the local system.<br/>
/// Exit code 0 (true) => <paramref name="taskName"/> exists<br/>
/// Exit code 1 (false) => <paramref name="taskName"/> does not exist<br/>
/// </summary>
/// <remarks>
/// This method queries the local Windows Task Scheduler using the schtasks.exe utility.
/// The check is case-insensitive and applies only to tasks on the local machine. If the task name contains path
/// separators (\), it is interpreted as a folder path within the Task Scheduler hierarchy.
/// </remarks>
/// <param name="taskName">The name of the scheduled task to check for existence. Cannot be null or empty.</param>
/// <returns>true if a scheduled task with the specified name exists; otherwise, false.</returns>
public static bool TaskExists(string taskName)
{
var psi = new ProcessStartInfo
{
FileName = "schtasks.exe",
Arguments = $"/query /tn \"{taskName}\"",
CreateNoWindow = true,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true
};
using var proc = Process.Start(psi);
proc.WaitForExit();
return proc.ExitCode == 0;
}
/// <summary>
/// Runs schtasks.exe with the given arguments.
/// Throws if exit code is non-zero.
/// </summary>
static void RunSchtasks(string arguments, bool logOutput = false)
{
var psi = new ProcessStartInfo
{
FileName = "schtasks.exe",
Arguments = arguments,
CreateNoWindow = true,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true
};
using var proc = Process.Start(psi);
proc.WaitForExit();
string output = proc.StandardOutput.ReadToEnd();
string error = proc.StandardError.ReadToEnd();
if (logOutput)
{
if (!string.IsNullOrWhiteSpace(output))
Logger.Write($"SchTasks StandardOutput: {output}", level: LogLevel.Info);
if (!string.IsNullOrWhiteSpace(error))
Logger.Write($"SchTasks StandardError: {error}", level: LogLevel.Info);
}
if (proc.ExitCode != 0)
{
throw new InvalidOperationException(
$"schtasks.exe failed.\nExitCode: {proc.ExitCode}\nOutput: {output}\nError: {error}");
}
}
/// <summary>
/// If creating a XML script to run as the current user, gets their SID.<br/>
/// See notes at top of module for example usage.<br/>
/// <code>
/// schtasks /create /tn "LaunchThenClose AutoStart" /xml LaunchThenCloseTask.xml /f
/// </code>
/// </summary>
/// <returns>Current user's SID</returns>
public static string GetCurrentUserSid()
{
// Can also get the user's SID via command line:
// C:\>wmic useraccount where name="%USERNAME%" get sid
try
{
return WindowsIdentity.GetCurrent().User.Value;
}
catch (Exception)
{
return string.Empty;
}
}
#region [XML Section]
/// <summary>
/// Creates a scheduled task using an XML template with injected parameters.
/// </summary>
public static bool CreateTaskFromXml(
string appName,
string exePath,
string workingDirectory,
string description,
string author,
string userSid = "S-1-5-18" // default SYSTEM
)
{
/*
ScheduledTaskHelper.CreateTaskFromXml(
appName: "AsyncTea",
exePath: @"C:\Apps\AsyncTea\AsyncTea.exe",
workingDirectory: @"C:\Apps\AsyncTea",
description: "Auto-start AsyncTea at logon",
author: Environment.UserName,
userSid: "S-1-5-18" // SYSTEM
);
*/
if (string.IsNullOrWhiteSpace(appName))
throw new ArgumentException("App name cannot be empty.", nameof(appName));
if (string.IsNullOrWhiteSpace(exePath))
throw new ArgumentException("Executable path cannot be empty.", nameof(exePath));
if (string.IsNullOrWhiteSpace(workingDirectory))
throw new ArgumentException("Working directory cannot be empty.", nameof(workingDirectory));
if (string.IsNullOrWhiteSpace(author))
throw new ArgumentException("Author cannot be empty.", nameof(author));
if (string.IsNullOrEmpty(userSid))
userSid = GetCurrentUserSid();
string xml = BuildXmlTemplate(appName, exePath, workingDirectory, description, author, userSid);
string xmlFile = $"{appName}.xml";
System.IO.File.WriteAllText(xmlFile, xml, Encoding.Unicode);
string taskName = $"{appName} AutoStart";
return RunXmlSchtasks($"/create /tn \"{taskName}\" /xml \"{xmlFile}\" /f", logOutput: true);
}
/// <summary>
/// Removes the scheduled task created earlier.
/// </summary>
public static bool RemoveTask(string appName)
{
string taskName = $"{appName} AutoStart";
return RunXmlSchtasks($"/delete /tn \"{taskName}\" /f", logOutput: true);
}
/// <summary>
/// Builds the XML template with injected parameters.
/// </summary>
static string BuildXmlTemplate(
string appName,
string exePath,
string workingDirectory,
string description,
string author,
string userSid
)
{
/*
<Priority>
✔ 0 => Idle
✔ 1 => Below Normal
✔ 2 => Normal
✔ 3 => Above Normal
✔ 4 => High
✔ 5 => Real‑time
✔ 6-10 => Reserved / treated as Normal (Scheduler ignores differences)
<ExecutionTimeLimit>
✔ PT0S
Means no time limit (run indefinitely). This is the most common value for background utilities.
✔ PTxxS
Run for N seconds. Example: PT30S → 30 seconds
✔ PTxxM
Run for N minutes. Example: PT10M → 10 minutes
✔ PTxxH
Run for N hours. Example: PT2H → 2 hours
✔ PxxD
Run for N days. Example: P1D → 1 day
✔ Full combinations
Example: P1DT2H30M10S → 1 day, 2 hours, 30 minutes, 10 seconds
<RunLevel>
✔ LeastPrivilege
- Runs the task without elevation
- Equivalent to running normally as the user
- UAC is not bypassed
- The task will run with whatever rights the user already has
This is the default if <RunLevel> is omitted.
✔ HighestAvailable
- Runs the task with the highest privileges the account can obtain
- If the account is an administrator → runs elevated
- If the account is standard → runs non‑elevated
- This is the setting you use for auto‑elevated startup apps
*/
return $@"<?xml version=""1.0"" encoding=""UTF-16""?>
<Task version=""1.4"" xmlns=""http://schemas.microsoft.com/windows/2004/02/mit/task"">
<RegistrationInfo>
<Date>{DateTime.UtcNow:yyyy-MM-ddTHH:mm:ss}</Date>
<Author>{author}</Author>
<Description>{description}</Description>
</RegistrationInfo>
<Triggers>
<LogonTrigger>
<Enabled>true</Enabled>
</LogonTrigger>
</Triggers>
<Principals>
<Principal id=""Author"">
<UserId>{userSid}</UserId>
<RunLevel>HighestAvailable</RunLevel>
</Principal>
</Principals>
<Settings>
<MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
<DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
<StopIfGoingOnBatteries>false</StopIfGoingOnBatteries>
<AllowHardTerminate>true</AllowHardTerminate>
<StartWhenAvailable>true</StartWhenAvailable>
<RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
<IdleSettings>
<StopOnIdleEnd>false</StopOnIdleEnd>
<RestartOnIdle>false</RestartOnIdle>
</IdleSettings>
<AllowStartOnDemand>true</AllowStartOnDemand>
<Enabled>true</Enabled>
<Hidden>false</Hidden>
<RunOnlyIfIdle>false</RunOnlyIfIdle>
<WakeToRun>false</WakeToRun>
<ExecutionTimeLimit>PT0S</ExecutionTimeLimit>
<Priority>7</Priority>
</Settings>
<Actions Context=""Author"">
<Exec>
<Command>{exePath}</Command>
<WorkingDirectory>{workingDirectory}</WorkingDirectory>
</Exec>
</Actions>
</Task>";
}
/// <summary>
/// Runs schtasks.exe with the given arguments.
/// </summary>
static bool RunXmlSchtasks(string arguments, bool logOutput = false)
{
var psi = new ProcessStartInfo
{
FileName = "schtasks.exe",
Arguments = arguments,
CreateNoWindow = true,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true
};
using var proc = Process.Start(psi);
proc.WaitForExit();
string output = proc.StandardOutput.ReadToEnd();
string error = proc.StandardError.ReadToEnd();
if (logOutput)
{
if (!string.IsNullOrWhiteSpace(output))
Logger.Write($"SchTasks StandardOutput: {output}", level: LogLevel.Info);
if (!string.IsNullOrWhiteSpace(error))
Logger.Write($"SchTasks StandardError: {error}", level: LogLevel.Info);
}
if (proc.ExitCode != 0)
{
throw new InvalidOperationException(
$"schtasks.exe failed.\nExitCode: {proc.ExitCode}\nOutput: {output}\nError: {error}");
}
return true;
}
#endregion
}
}