guitarrapc / GraniResource

PowerShell Desired State Configuration Resources for real production workload.
https://www.powershellgallery.com/packages/GraniResource
MIT License
43 stars 8 forks source link

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

Open Zuldan opened 8 years ago

Zuldan commented 8 years ago

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.

guitarrapc commented 8 years ago

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?

Zuldan commented 8 years ago

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

Zuldan commented 8 years ago

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 commented 8 years ago

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