Retry attempts on failure when installing scheduled task with PowerShell

眉间皱痕 提交于 2021-02-07 09:30:39

问题


I am installing a scheduled task using a PowerShell script, but I would like it to retry 3 times on failure, how can I add this to my scheduled task registration script?

$dropLocation = "C:\Tasks\"
$Action = New-ScheduledTaskAction -Execute "$dropLocation\Task.exe" 
$Trigger = New-ScheduledTaskTrigger -Daily -At 10:15pm
$Settings = New-ScheduledTaskSettingsSet -RestartCount:3
Register-ScheduledTask -Action $Action -Trigger $Trigger -TaskName "$taskName" -Settings $Settings -Description "TaskDescription" 

回答1:


Specifying the retry count is not enough. You have to specify two parameters:
The retry count and the retry interval.

To retry up to three times with an interval of 1 minute, your settings should look like this:

$Settings = New-ScheduledTaskSettingsSet -RestartCount:3 -RestartInterval (New-TimeSpan -Minutes 1)


来源:https://stackoverflow.com/questions/35082121/retry-attempts-on-failure-when-installing-scheduled-task-with-powershell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!