[英]How can I define an expiry date to a scheduled task using PowerShell
Is there a way I can schedule a task using PowerShell that for 30 days runs on daily basis every 15 mins, and after those 30 days it expires?有没有一种方法可以使用 PowerShell 安排任务,该任务每天每 15 分钟运行 30 天,然后在 30 天后到期?
I have done the scheduling but I am unable to find anything where we can set an expiry date using New-ScheduledTaskSettingsSet
.我已经完成了计划,但我找不到任何可以使用
New-ScheduledTaskSettingsSet
设置到期日期的东西。
Code:代码:
$trigger = New-ScheduledTaskTrigger -Daily -At 9am
$principal = New-ScheduledTaskPrincipal -UserId $user -RunLevel Highest -LogonType ServiceAccount
$task = Register-ScheduledTask -TaskName 'KView' -Description 'Kview setup for monitoring' -Action $action -Trigger $trigger -Principal $principal
#$task.Triggers.StartBoundary = $date
#$task.Triggers.EndBoundary = $date.AddDays(30)
$task.Triggers.Repetition.Duration = 'P1D'
$task.Triggers.Repetition.Interval = 'PT15M'
$task | Set-ScheduledTask -User $user -Password $password
Below is an example of the code that will set the expiry for you, just change the TargetTask
name and EndBoundary
value.下面是为您设置到期时间的代码示例,只需更改
TargetTask
名称和EndBoundary
值。
Notice that Triggers are arrays.请注意,触发器是数组。 This scheduled task only has one trigger, so we leave it as
$Task.Triggers[0]
.此计划任务只有一个触发器,因此我们将其保留为
$Task.Triggers[0]
。
# Fetch the scheduled task object
$Task = Get-ScheduledTask -TaskName "Test"
# Set the date and time it will expire
$Task.Triggers[0].EndBoundary = [DateTime]::Now.AddDays(1).ToString("yyyy-MM-dd'T'HH:mm:ss")
$Task | Set-ScheduledTask
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.