我有一些使用计划任务每天运行的PowerShell脚本,计划任务是使用PowerShell本身创建的,并且运行良好。
我希望能够将设置添加到计划任务中,如下所示: -
我希望能够更改设置 - >如果任务超过3天,则停止该任务。
我能否以某种方式更改此设置,我已设法在PowerShell中创建计划任务,我可以更改触发器,操作等但我无法看到如何影响设置,而无需手动更改任务内部任务调度器。
答案 0 :(得分:0)
帮助:
http://powershell.com/cs/blogs/tips/archive/2013/06/21/changing-scheduled-tasks-with-powershell.aspx
<强>解决方案:强>
$service = New-Object -ComObject Schedule.Service
# Can connect remotely as well, currently doing it local
$service.Connect($env:COMPUTERNAME)
# navigate to desired folder where the scheduled task is in TaskScheduler
$folder = $service.GetFolder('\Microsoft\Windows\XXX_DestinationFolder_XXX')
# Get the desired task
$task = $folder.GetTask('XXX_TaskName_XXX')
$definition = $task.Definition
# Incase you are interested in changing more settings
# $definition | Get-Member
# for 10 days it is 'P10D', by default it is 3 days so it will be 'P3D'
$definition.Settings | Where-Object {$_.ExecutionTimeLimit = 'P10D'}
# finally updating
$folder.RegisterTaskDefinition($task.Name, $definition, 4, $null, $null, $null)
答案 1 :(得分:0)
如果您可以升级到PowerShell 4.0或更高版本,则可以使用ScheduledTask CMDlets创建/更新它们。以下是更改时间限制的示例:
$Global