Script to enable a task in task scheduler not working!

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
jameel
Posts: 11
Joined: 26 Dec 2020, 04:56

Script to enable a task in task scheduler not working!

Post by jameel » 20 Feb 2021, 08:22

Hello everyone!

I have created a script to run a task on task scheduler, which can open notepad program.

I imported the task with this code:

Code: Select all

run, schtasks /create /tn "Open Notepad" /xml "C:\Users\%user%\Downloads\A\Open Notepad.xml" /ru %computer_name%\username /rp password
And the task has been successfully imported.

But I tried to enable it using this code:

Code: Select all

!v::  ; Enable Task:
TaskEnabled("Test", "\", True)
return

!e::  ; Disable Task:
TaskEnabled("Test", "\", False)
return


; TaskName: Name of the Task.
; TaskFolder: Folder where the Task is located.
; Enabled: 'True' to enable Task or 'False' to disable it.

TaskEnabled(TaskName, TaskFolder="\", Enabled=True)
{
	Service := ComObjCreate("Schedule.Service")
	Service.Connect()
	Service.GetFolder(TaskFolder).GetTask(TaskName).Enabled := Enabled
}
When I hit "!v", the follwoing message appeared:
T.png
T.png (19.13 KiB) Viewed 628 times
I tried to replace "TaskName" and "TaskFolder" with the exact task name and its path to tasks folder inside System32, also the same dialogue box appeared.

Hope someone can help me.

Thanks in advance.
User avatar
mikeyww
Posts: 26848
Joined: 09 Sep 2014, 18:38

Re: Script to enable a task in task scheduler not working!

Post by mikeyww » 20 Feb 2021, 09:53

But your new task is not called "Test", right?

The following worked when I tested it.

Code: Select all

If !A_IsAdmin && !RegExMatch(DllCall("GetCommandLine", "str"), " /restart(?!\S)") {
 Try Run *RunAs "%A_AhkPath%" /restart "%A_ScriptFullPath%"
 ExitApp
}

TaskEnabled("Adobe Acrobat Update Task", "\", False)

TaskEnabled(TaskName, TaskFolder := "\", Enabled := True)
{
	Service := ComObjCreate("Schedule.Service")
	Service.Connect()
	Service.GetFolder(TaskFolder).GetTask(TaskName).Enabled := Enabled
}
Note Enabled := True, though it will not matter in this specific case.
jameel
Posts: 11
Joined: 26 Dec 2020, 04:56

Re: Script to enable a task in task scheduler not working!

Post by jameel » 20 Feb 2021, 10:57

Yes, my task name is "Open Notepad", not "Test"

Your script worked for me but when I tried to enable the task after importing it immediately, the following dialogur box appeared:
A.png
A.png (18.38 KiB) Viewed 603 times
I think it is because the script triggered quickly before it realizes that the task file exists

Can you fix this with a time interval between the two codes?
User avatar
mikeyww
Posts: 26848
Joined: 09 Sep 2014, 18:38

Re: Script to enable a task in task scheduler not working!

Post by mikeyww » 20 Feb 2021, 11:56

The following import routine worked for me.

Code: Select all

If !A_IsAdmin && !RegExMatch(DllCall("GetCommandLine", "str"), " /restart(?!\S)") {
 Try Run *RunAs "%A_AhkPath%" /restart "%A_ScriptFullPath%"
 ExitApp
}
task = Adobe Acrobat Update Task
xml  = e:\data\temp2\%task%.xml
RunWait, %A_WinDir%\System32\schtasks.exe /create /tn "%task%" /xml "%xml%" /ru SYSTEM,, Hide
jameel
Posts: 11
Joined: 26 Dec 2020, 04:56

Re: Script to enable a task in task scheduler not working!

Post by jameel » 20 Feb 2021, 22:59

It worked perfectly!

Thanks very much!
User avatar
DavidP
Posts: 225
Joined: 10 Aug 2014, 07:31

Re: Script to enable a task in task scheduler not working!

Post by DavidP » 12 Jun 2021, 16:24

Hello all,

I'm trying to delete multiple scheduled tasks that all begin with the string "Foobar".

On Stack Overflow I found this Windows CMD code:

Code: Select all

For /F "Tokens=1*Delims=\" %# In ('SchTasks /Query /FO List^|Find /I "Foobar"')Do @SchTasks /Delete /TN "%$" /F
Can the above be run directly in/from an AutoHotkey script, like the basic command below can?

Code: Select all

Run Schtasks /delete /tn "\Foobar-123" /F
User avatar
mikeyww
Posts: 26848
Joined: 09 Sep 2014, 18:38

Re: Script to enable a task in task scheduler not working!

Post by mikeyww » 12 Jun 2021, 17:30

You could try it like the ComSpec example, in case that works.
User avatar
DavidP
Posts: 225
Joined: 10 Aug 2014, 07:31

Re: Script to enable a task in task scheduler not working!

Post by DavidP » 13 Jun 2021, 04:42

Thank you, I will try that.
Post Reply

Return to “Ask for Help (v1)”