GraniResource icon indicating copy to clipboard operation
GraniResource copied to clipboard

[Question] How to specify day to run scheduled task?

Open Zuldan opened this issue 8 years ago • 4 comments

Hi guitarrapc, any tips on how to configure a scheduled task to run for example every Saturday at 2AM?

I can't figure out how to specify what day to run the scheduled task on.

Zuldan avatar Mar 16 '16 06:03 Zuldan

Hi Zuldan,

Let's try set ScheduledAt as past date for Saturday AM2:00:00, like ScheduledAt = [DateTime]"2016/02/13 02:00:00". Then use RepetitionIntervalTimeSpanString as 1week, like RepetitionIntervalTimeSpanString = "7.00:00:00".

Code looks like this.

configuration Sample
{
    Import-DscResource -ModuleName GraniResource;
    cScheduleTask 1WeekSample
    {
        Ensure = "Present"
        TaskName = "Sample"
        TaskPath = "\"
        ScheduledAt = [DateTime]"2016/02/13 02:00:00" # Means Saturday
        RepetitionIntervalTimeSpanString = [TimeSpan]::FromDays(7).ToString() # run every week
        RepetitionDurationTimeSpanString = [TimeSpan]::MaxValue.ToString() # forever
        Execute = "powershell.exe"
        Argument = "-Command Get-Date"
        Disable = $true
        Hidden = $true
        Compatibility = "Win8"
        Runlevel = "Highest"
    }
}

Is it answer your question?

guitarrapc avatar Mar 16 '16 07:03 guitarrapc

Hi guitarrapc, I will play around with the code tomorrow and see how I go. Thank you for the fast response!

Zuldan avatar Mar 16 '16 11:03 Zuldan

Hi guitarrapc,

After investigating it appears the resource is not creating a true 'Weekly' trigger. It's creating a 'One time'. Maybe you could add this functionality like so...

configuration Sample { Import-DscResource -ModuleName GraniResource; cScheduleTask 1WeekSample { Ensure = "Present" TaskName = "Sample" TaskPath = "" ScheduledAt = [DateTime]"02:00:00" Weekly = $True Day = 'Saturday' Execute = "powershell.exe" Argument = "-Command Get-Date" Disable = $true Hidden = $true Compatibility = "Win8" Runlevel = "Highest" } }

We're using this resource on 200+ servers and we need to change a current scheduled task from daily or weekly. I'm worried when admin's view the task settings and see "Run once" they might get confused.

On another note, have you considered contributing this resource to Microsoft? it's the best ScheduledTask DSC resource on the Internet and you already have all the Pester tests for it so I'm sure Microsoft would gladly accept it. Your code is beautiful!

Zuldan avatar Mar 17 '16 06:03 Zuldan

Hi guittarpc, any chance of adding Day = to the options ?

Zuldan avatar Mar 28 '16 20:03 Zuldan