2

I am attempting to use schtasks to run a once off task that will simply execute a program. Here is what it looks like:

schtasks /Create /SC ONCE /TN "OpenCalc" /TR "C:\Windows\System32\calc.exe" /ST "11:59" /SD "09/12/2018"

This works. It creates a task (can be seen in Windows Task Scheduler), and it executes.

I want this task to delete itself after its one execution, so I add the following:

/Z

A value that marks the task to be deleted after its final 

Now my command is:

schtasks /Create /SC ONCE /TN "OpenCalc" /TR "C:\Windows\System32\calc.exe" /ST "11:59" /SD "09/12/2018" /Z

However, now I get the following error:

ERROR: The task XML is missing a required element or attribute.
(40,4):EndBoundary:

The documentation would lead me to believe the /Z switch has nothing to do with the /XML parameter, but maybe this is not the case. Am I missing something here?

Damien
  • 624
  • 7
  • 25
  • 3
    I believe [Super User](https://superuser.com/help/on-topic) is a more appropriate site for this question. This has already been asked there as [How do you schedule a task (using schtasks.exe) to run once and delete itself?](https://superuser.com/q/1038528/34985). – Lance U. Matthews Oct 09 '18 at 01:38

3 Answers3

3

The option /z does not work with starttype once, see this answer. With option /v1 the task is created successfully, but not deleted after running.

You may delete the task 'maually', eg.

... /TR "cmd.exe /c C:\Windows\System32\calc.exe && schtasks /delete /tn OpenCalc /f"
some guy
  • 166
  • 4
  • Hi, i would like my schtask delete itself after run as well, i have /sc ONIDLE been set but the schtask doesn't run with "&& schtask /delete......". My script is schtasks /create /TN "testing1" /TR "ngen install file && schtasks /delete /TN "testing1"" /SC ONIDLE /I 1 /RL HIGHEST /F. It looks similar to your answer here but with && delete my first part of schtask(ngen install) couldn't run either. Wonder if you have any ideas? Thanks in advanced! – Scott Tan Jun 01 '20 at 05:07
  • ok, & and && only work in Windows' cmd.exe so my case wont work with &&. – Scott Tan Jun 01 '20 at 06:27
  • Hi @ScottTan. Yes, you are right. The trick is to have one command (with /TR), which executes the desired action and afterwards delete iteself. To concatenate executables you can use cmd.exe, check [this](https://stackoverflow.com/a/8055430/12523167). Also be careful with the double quotes, it's easier if your tasks name doesn't contain spaces and so you can write the name without double quotes. Otherwise in the command you have to escape the double quotes. – some guy Jun 02 '20 at 06:57
1

This issue occurs because of changes in the Task Scheduler service in Windows Vista.

(Source)

As a workaround, Microsoft recommends the following:

To resolve this issue, use the /V1 switch. The /V1 switch creates a task that is compatible with pre-Windows Vista platforms.

Damien
  • 624
  • 7
  • 25
1

"This issue occurs because of changes in the Task Scheduler service in Windows Vista."

It also occurs in Windows 10 (1903 update) however the the "V1" option will allow the task to be created. It also requires the user to enter a password.

If you are using a cmd file for your task and you know the taskname then this does work:

Schtasks.exe /Delete /TN taskname < yes.txt

Where yes.txt is a text file with the single character "Y". It simulates a response to the confirmation prompt (it is the same thing we did back in the days of MS-DOS 3.X to confirm file deletions or overwrites in batch files).

C:\Users\John Doe> Schtasks.exe /Delete /TN k138A < yes.txt
WARNING: Are you sure you want to remove the task "k138A" (Y/N)? Y
SUCCESS: The scheduled task "k138A" was successfully deleted.

I believe schtasks.exe was written for the Windows Scheduler 1.2 API thus it will always create a 'vista' level task anyway. It is possible/probable that Microsoft will never update schtasks.exe to the 2.0 API so that they can "encourage" people to move the PowerShell Scheduler cmdlets.

https://learn.microsoft.com/en-us/powershell/module/scheduledtasks/?view=win10-ps

LewTwo
  • 83
  • 8