简体   繁体   中英

Windows 10 Schtasks.exe Delete After Final Run Error

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?

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"

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.

"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://docs.microsoft.com/en-us/powershell/module/scheduledtasks/?view=win10-ps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM