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 can't disable default ScheduledDefrag task #137

Open
RobBiddle opened this issue Jan 28, 2018 · 10 comments
Open

xScheduledTask can't disable default ScheduledDefrag task #137

RobBiddle opened this issue Jan 28, 2018 · 10 comments
Labels
bug The issue is a bug. duplicate The issue or PR is the duplicate of another. in progress The issue is being actively worked on by someone.

Comments

@RobBiddle
Copy link

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
Copy link
Author

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
Copy link
Author

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

@RobBiddle
Copy link
Author

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                
            }
        }

@PlagueHO
Copy link
Member

Hi @RobBiddle - thanks for submitting this. I could be wrong, but I think this is a duplicate of #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: #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 PlagueHO added bug The issue is a bug. duplicate The issue or PR is the duplicate of another. labels Jan 28, 2018
@PlagueHO
Copy link
Member

PlagueHO commented Feb 8, 2018

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!

@adriandeller
Copy link

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?

@matt6697
Copy link
Contributor

@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
Copy link
Contributor

@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

@PlagueHO
Copy link
Member

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 PlagueHO added the in progress The issue is being actively worked on by someone. label Feb 20, 2018
@EDockus
Copy link

EDockus commented Jun 25, 2019

I've had a very similar issue. Created a PR that has fixed it for me - #228

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. duplicate The issue or PR is the duplicate of another. in progress The issue is being actively worked on by someone.
Projects
None yet
Development

No branches or pull requests

5 participants