ComputerManagementDsc icon indicating copy to clipboard operation
ComputerManagementDsc copied to clipboard

xScheduledTask: daily triggers fall out of compliance each day

Open FinickyCode opened this issue 7 years ago • 4 comments

It seems that scheduled tasks with a daily schedule get deleted and recreated every day, on the first DSC run after midnight. It appears to me that the date in the daily trigger is compared to the current date.

You can reproduce this by creating a task with a daily trigger, then manually change the date in the daily trigger, and Test-DscConfiguration will return False, setting the date back to the current date will result in Test-DscConfiguration returning True.

Is this expected? Seems to me that the date on the daily trigger should not matter if it is the current date or earlier.

Configuration:

    xScheduledTask 'mytask'
    {
        Ensure                  = 'Present'
        TaskName                = 'mytask'
        Description             = 'Collect system information'
        ActionExecutable        = 'c:\windows\system32\WindowsPowerShell\v1.0\powershell.exe'
        ActionArguments         = '-NonInteractive -File c:\scripts\mytask.ps1 -output c:\temp\mytask\ -rsync -compress -verbose'
        ActionWorkingPath       = 'c:\scripts'
        ScheduleType            = 'Daily'
        DaysInterval            = 1
        StartTime               = '23:30:00'
        RandomDelay             = '01:00:00'
        ExecutionTimeLimit      = '06:00:00'
        Enable                  = $true
        MultipleInstances       = 'IgnoreNew'
        Priority                = 9
        Compatibility           = 'Win8'
    }

Windows version: Server 2016, 10.0.14393.0 PowerShell version:

>$PSVersionTable

Name                           Value
----                           -----
PSVersion                      5.1.14393.2068
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0, 5.0, 5.1.14393.2068}
BuildVersion                   10.0.14393.2068
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

DSC module version: 4.0.0.0

FinickyCode avatar Mar 09 '18 04:03 FinickyCode

Hi FinickyCode,

I've got the same issue. It looks like it takes StartTime ('23:30:00') and converts to the time of the current date, but when you apply your configuration, it compares the time with the time when the scheduled task was created. See my configuration and output when I apply the configuration next day:

xScheduledTask BackupSqlDbFull
        {
            DependsOn = '[xRemoteFile]BackupSqlDb'
            TaskName = 'Backup-SqlDb (Full)'
            ActionExecutable = 'C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe'
            ActionArguments = '-File "' + $Node.BackupSqlDbScriptDstPath + '"'
            ScheduleType = 'Weekly'
            DaysOfWeek = 'Monday'
            WeeksInterval = 1
            StartTime = [DateTime]'04:00:00'
        }

[[xScheduledTask]BackupSqlDbFull] NOTMATCH: Value (type 'DateTime') for property 'StartTime' does not match. Current state is '3/26/2018 4:00:00 AM' and desired state is '3/27/2018 4:00:00 AM'.

As a workaround, I specified exact date and time which helped: StartTime = [DateTime]'2018-03-26T04:00:00'

It is an interesting question how the issue will be solved by Powershell Team.

VitaliHarbiankou avatar Mar 27 '18 09:03 VitaliHarbiankou

Hi @FinickyCode and @VitaliHarbiankou - thanks for logging this and all the detailed info. I'm going to try and get some time this weekend (it's a long weekend here) to put into cleaning up some of these issues in xScheduledTask. Thanks for your patience on this one.

PlagueHO avatar Mar 28 '18 07:03 PlagueHO

@VitaliHarbiankou you are right on the money, I can't believe I didn't think of that, thank you. I can't imagine that not solving my issue. I've updated all my configurations to have a StartTime with a fixed date and will see how it goes.

In terms of helping future users that might encounter this, can I suggest the following:

  • Change the default StartTime from [DateTime]::Today to [DateTime]'2016-01-01'
  • Perform a check in the code to see if $StartTime.Date -eq [DateTime]::Today and if so then move the date back, i.e. ([DateTime]'2016-01-01').Add($StartTime.TimeOfDay)

I think those two things will address the issue as I see it: the default value will be a fixed date in the past, and if someone specifies just a time for StartTime, it will default to a fixed date in the past also. This still allows setting a fixed date in the future (which I guess someone may want), and in that scenario, it will also work in a manner that I'd consider desirable.

Any downsides or things I've missed? @PlagueHO I'm happy to do a pull request if this is a reasonable approach.

FinickyCode avatar Mar 28 '18 09:03 FinickyCode

See also Issue #251.

General-Fault avatar Dec 11 '20 20:12 General-Fault