简体   繁体   中英

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?

I have done the scheduling but I am unable to find anything where we can set an expiry date using 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.

Notice that Triggers are arrays. This scheduled task only has one trigger, so we leave it as $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

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