Reputation: 31
I have an electron app through which I want to schedule a task on windows. The task I want to perform is to trigger an my electron app with admin access on system restart. This is my code
const { exec } = require('child_process');
function addToTaskScheduler() {
// Path to my app
const appPath = `"${process.execPath}"`;
const taskName = "MyApp";
// schtasks command to create a task
const schtasksCommand = `schtasks /create /tn "${taskName}" /tr ${appPath} /sc onlogon /ru "NT AUTHORITY\\SYSTEM" /rl highest /f`;
exec(schtasksCommand, (error, stdout, stderr) => {
if (error) {
console.error('Error creating task:', error.message);
} else if (stderr) {
console.error('Task creation stderr:', stderr);
} else {
console.log('Task created successfully:', stdout);
}
});
}
this is successfully creating a task but the problem is it is not invoking the app in system reboot(on user login).
I have checked the task settings, it has "run with highest privileges" & "run only when the user logged on" checked. Also the app path is correctly enclosed in quotes and it is pointing to the correct app path. Even if I manually click on run the task it is not launching my application.
I have checked windows logs viewer and there it is showing task has started successfully but still not launching the application.
Task Scheduler successfully finished "{69373607-3cbe-4cd8-a718-c5bcb3be986f}" instance of the "\MyApp" task for user "NT AUTHORITY\SYSTEM".
<Event xmlns="http://schemas.microsoft.com/win/2004/08/events/event">
<System>
<Provider Name="Microsoft-Windows-TaskScheduler" Guid="{de7b24ea-73c8-4a09-985d-5bdadcfa9017}" />
<EventID>102</EventID>
<Version>0</Version>
<Level>4</Level>
<Task>102</Task>
<Opcode>2</Opcode>
<Keywords>0x8000000000000001</Keywords>
<TimeCreated SystemTime="2025-01-03T14:21:09.6453464Z" />
<EventRecordID>282</EventRecordID>
<Correlation ActivityID="{69373607-3cbe-4cd8-a718-c5bcb3be986f}" />
<Execution ProcessID="1928" ThreadID="2160" />
<Channel>Microsoft-Windows-TaskScheduler/Operational</Channel>
<Computer>DESKTOP-GA6PQ7E</Computer> <Security UserID="S-1-5-18" />
</System>
<EventData Name="TaskSuccessEvent">
<Data Name="TaskName">\MyApp</Data>
<Data Name="UserContext">NT AUTHORITY\SYSTEM</Data>
<Data Name="InstanceId">{69373607-3cbe-4cd8-a718-c5bcb3be986f}</Data>
</EventData>
</Event>
This is perfectly working fine in some windows systems(win 11 home and win 10 pro but not working on windows 11 pro). what am I doing wrong?
Upvotes: 2
Views: 28