Wednesday, February 10, 2016

If you want to have a task deleted automatically after it runs, you must add at least one trigger that has an expiration date.

Issue:
When attempting to use Task Scheduler with the "/sc onstart" and "/z" switches, the task does not get deleted. The error "If you want to have a task deleted automatically after it runs, you must add at least one trigger that has an expiration date." may occur. This occurs when trying to run a scheduled task on startup (onstart) and have it deleted upon completion (/z). Although a deletion of the task could be performed via the GUI, the point is to have the task removed automatically.
Environment:
Windows. The goal is to kickoff a task when a computer is rebooted, have the task complete and then be deleted so that it doesn't run a second time.
Symptoms:
Task runs, however does not get deleted upon completion.
Solution:
It appears that to use the "/sc once" switch, a time MUST be entered. Without knowing the time that the system will be rebooted, this isn't easy or practical to do. Using the "/sc onstart" option with "/z" doesn't produce the desired results either (as mentioned above). The solution was to have the task delete itself.
The first task can be created such as:
SCHTASKS /create /sc onstart /tn "myTask" /tr "C:\temp\test.bat" /ru SYSTEM
The test.bat file would accomplish whichever tasks were desired, and then delete the task:
test.bat:
****
...Actions to take...
SCHTASKS /delete /tn "myTask" /f
****

Please note that this issue is different from the error "The task XML is missing a required element or attribute" which occurs if not using the /v1 switch, as described here:
https://support.microsoft.com/en-us/kb/2004151