ComputerManagementDsc icon indicating copy to clipboard operation
ComputerManagementDsc copied to clipboard

xScheduledTask can't disable default ScheduledDefrag task

Open RobBiddle opened this issue 7 years ago • 10 comments

I'm using xComputerManagement 3.2.0.0, attempting to disable the default task ScheduledDefrag

I've tried populating all of the current values for the task, only changing Enable to $false and ScheduleType to 'Once'.

Here is the configuration and error thrown:

        {
            ActionArguments                 = '-c -h -k -g -$'
            ActionExecutable                = '%windir%\system32\defrag.exe'
            AllowStartIfOnBatteries         = $false
            Compatibility                   = 'Win8'
            Description                     = 'This task optimizes local storage drives.'
            DisallowDemandStart             = $false
            DisallowHardTerminate           = $false
            DisallowStartOnRemoteAppSession = $false
            DontStopIfGoingOnBatteries      = $false
            DontStopOnIdleEnd               = $false
            Enable                          = $false
            Ensure                          = 'Present'
            ExecutionTimeLimit              = '3.00:00:00'
            Hidden                          = $false
            IdleDuration                    = '00:00:00'
            IdleWaitTimeout                 = '00:00:00'
            LogonType                       = 'ServiceAccount'
            MultipleInstances               = 'IgnoreNew'
            Priority                        = '7'
            RandomDelay                     = '00:00:00'
            RepeatInterval                  = '00:00:00'
            RepetitionDuration              = 'Indefinitely'
            RestartCount                    = '0'
            RestartInterval                 = '00:00:00'
            RestartOnIdle                   = $false
            RunLevel                        = 'Highest'
            RunOnlyIfIdle                   = $false
            RunOnlyIfNetworkAvailable       = $false
            ScheduleType                    = 'Once'
            StartTime                       = '1/28/2018 12:00:00 AM'
            TaskName                        = 'ScheduledDefrag'
            TaskPath                        = '\Microsoft\Windows\Defrag\'
            User = 'SYSTEM'
            WakeToRun                       = $false
        }


VERBOSE: [ADM3]: LCM:  [ End    Test     ]  [[xScheduledTask]DisableScheduledDefragTask]  in 0.4430 seconds.
PowerShell DSC resource MSFT_xScheduledTask  failed to execute Test-TargetResource functionality with error message: Trigger type  not recognized.
Parameter name: CimClassName
    + CategoryInfo          : InvalidOperation: (:) [], CimException
    + FullyQualifiedErrorId : ProviderOperationExecutionFailure
    + PSComputerName        : localhost```

RobBiddle avatar Jan 28 '18 19:01 RobBiddle

I initially tried using a minimal configuration, which also failed

        xScheduledTask DisableScheduledDefragTask
        {
            TaskName            = 'ScheduledDefrag'
            TaskPath            = '\Microsoft\Windows\Defrag\'
            ActionExecutable    = '%windir%\system32\defrag.exe'
            ScheduleType        = 'Once'
            Enable              = $false
        }

RobBiddle avatar Jan 28 '18 19:01 RobBiddle

I'm pretty sure it's because the task has no triggers.

RobBiddle avatar Jan 28 '18 19:01 RobBiddle

For anyone else needing to do this, the workaround is a Script Resource, which actually took me less time to write then the definition for xScheduledTask, lol

        Script DisableScheduledDefragTask {
            GetScript  = {
                Get-ScheduledTask ScheduledDefrag
            }
            SetScript  = {
                Get-ScheduledTask ScheduledDefrag | Disable-ScheduledTask
            }
            TestScript = {
                -NOT (Get-ScheduledTask ScheduledDefrag).Settings.Enabled                
            }
        }

RobBiddle avatar Jan 28 '18 19:01 RobBiddle

Hi @RobBiddle - thanks for submitting this. I could be wrong, but I think this is a duplicate of https://github.com/PowerShell/xComputerManagement/issues/74. I just finished the fix for this a couple of weeks ago and it should go out to the resource kit in the next release. Here is the PR that contained the fix: https://github.com/PowerShell/xComputerManagement/pull/132

I'll leave this open for a little bit, but if the new release does fix the issue then we should be able to close it.

PlagueHO avatar Jan 28 '18 20:01 PlagueHO

Hi @RobBiddle - the new release of xComputerManagement went out today with the fix you need (hopefully). Could you give this a try and see if it solves the issue for you? Thanks!

PlagueHO avatar Feb 08 '18 07:02 PlagueHO

I've installed https://www.powershellgallery.com/packages/xComputerManagement/4.0.0.0 and still can not disable the built-in scheduled task "XblGameSaveTask". PowerShell DSC resource MSFT_xScheduledTask failed to execute Test-TargetResource functionality with error message: Trigger type 'MSFT_TaskIdleTrigger' not recognized. Parameter name: CimClassName + CategoryInfo : InvalidOperation: (:) [], CimException + FullyQualifiedErrorId : ProviderOperationExecutionFailure + PSComputerName : localhost Any ideas or suggestions?

adriandeller avatar Feb 12 '18 12:02 adriandeller

@PlagueHO : Hi Daniel, I installed 4.0.0.0 version to test the deletion of built-in scheduled tasks : it works well for tasks with known Trigger types and helps a lot for my Citrix VDA optimization (https://github.com/virtualdesktopdevops/xd7vda)

However, it fails with the following error if the Trigger type is unknown : PowerShell DSC resource MSFT_xScheduledTask failed to execute Test-TargetResource functionality with error message: Trigger type MSFT_TaskEventTrigger not recognized.\r\nParameter name: CimClassName

It think that the correct behavior would be to delete the scheduled task, even if the Trigger type is unknown. Do you agree with this ?

matt6697 avatar Feb 13 '18 21:02 matt6697

@PlagueHO : Hi Daniel, just created PR #142 to correct this issue. Could you have a look at it ? I don't know if one of the pester tests has to be updated to reflect this new behavior (not throwing an error anymore). Cheers

matt6697 avatar Feb 15 '18 18:02 matt6697

Hi @matt6697 - sorry about the delay in getting to look at this! But I'm on it now. We don't actually have test coverage hitting this line, but it would be good to get a test for it so we don't regress at any point.

You probably just need a test like this:

            Context 'When a built-in scheduled task exists and is enabled, but it should be disabled and the trigger type is not recognized' {
                $testParameters = @{
                    TaskName = 'Test task'
                    TaskPath = '\Test\'
                    Enable   = $false
                    Verbose  = $True
                }

                Mock -CommandName Get-ScheduledTask -MockWith {
                    @{
                        TaskName = $testParameters.TaskName
                        TaskPath = $testParameters.TaskPath
                        Actions  = [pscustomobject] @{
                            Execute = 'C:\windows\system32\WindowsPowerShell\v1.0\powershell.exe'
                        }
                        Triggers = [pscustomobject] @{
                            Repetition = @{
                                Duration = "PT15M"
                                Interval = "PT15M"
                            }
                            CimClass   = @{
                                CimClassName = 'MSFT_TaskEventTrigger'
                            }
                        }
                        Settings = [pscustomobject] @{
                            Enabled = $true
                        }
                    } }

                It 'Should return the correct values from Get-TargetResource' {
                    $result = Get-TargetResource @testParameters
                    $result.Enable | Should -Be $true
                    $result.Ensure | Should -Be 'Present'
                    $result.ScheduleType | Should -BeNullOrEmpty
                }

                It 'Should return false from the test method' {
                    Test-TargetResource @testParameters | Should -Be $false
                }

                It 'Should disable the scheduled task in the set method' {
                    Set-TargetResource @testParameters
                    Assert-MockCalled Register-ScheduledTask -Exactly -Times 1
                }
}

PlagueHO avatar Feb 16 '18 19:02 PlagueHO

I've had a very similar issue. Created a PR that has fixed it for me - https://github.com/PowerShell/ComputerManagementDsc/pull/228

EDockus avatar Jun 25 '19 15:06 EDockus