Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

xScheduledTask: daily triggers fall out of compliance each day #148

Open
FinickyCode opened this issue Mar 9, 2018 · 4 comments · May be fixed by #440
Open

xScheduledTask: daily triggers fall out of compliance each day #148

FinickyCode opened this issue Mar 9, 2018 · 4 comments · May be fixed by #440
Labels
bug The issue is a bug. help wanted The issue is up for grabs for anyone in the community.

Comments

@FinickyCode
Copy link

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

@VitaliHarbiankou
Copy link

VitaliHarbiankou commented Mar 27, 2018

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.

@PlagueHO PlagueHO added bug The issue is a bug. help wanted The issue is up for grabs for anyone in the community. labels Mar 28, 2018
@PlagueHO
Copy link
Member

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.

@FinickyCode
Copy link
Author

@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.

@General-Fault
Copy link

See also Issue #251.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug The issue is a bug. help wanted The issue is up for grabs for anyone in the community.
Projects
None yet
4 participants