Hi all,
Here is the sample PowerShell script to configure schedule task that runs every 5 minutes for 3 years.
You can change the years , why did I not mentioned infinite time is because of some limitation that I had explained in the other article.
Script:
$mycredentials = Get-Credential
Invoke-Command -ComputerName “Server1”,”Server2” -Credential $mycredentials -ScriptBlock {
$dt= ([DateTime]::Now)
$timespan = $dt.AddYears(3) -$dt;
$Action = New-ScheduledTaskAction -Execute ‘powershell.exe’ -Argument ‘-command “D:\PS_Jobs\PS_Job1.ps1” -ExecutionPolicy RemoteSigned -NoProfile’
$Trigger = New-ScheduledTaskTrigger -Once -At 9am -RandomDelay (New-TimeSpan -Minutes 30) -RepetitionDuration $timespan -RepetitionInterval (New-TimeSpan -Minutes 60)
$Task = New-ScheduledTask -Action $Action -Trigger $Trigger -Settings (New-ScheduledTaskSettingsSet)
$Task | Register-ScheduledTask -TaskName ‘Job1 Task’
}
Regards,
Chaitanya