How To: Enable Defrag on Server 2008R2 and Windows 7 Using ConfigMgr Setttings Management

We found several systems recently where defrag was not enabled. Thanks to a little help from the TechNet forums, I was able to create a Compliance Settings Rule using PowerShell. Create a new compliance settings configuration item as follows:

      1. Choose a name (Such as Enable Defrag).
      2. Choose the supported platforms (all Windows 7, all Server 2008 R2)
      3. Create a new Setting, choose Script for the setting type, and Boolean for the data type.
      4. Paste the PowerShell code below for the Discovery Script:
        # Used by the RegisterTaskDefinition method
        $TASK_CREATE_OR_UPDATE = 0x6
        
        # Specify the computer name
        $computerName = "localhost"
        # Specify the task's name
        $taskName = "\Microsoft\Windows\Defrag\ScheduledDefrag"
        
        $taskService = new-object -comobject "Schedule.Service"
        $taskService.Connect($computerName)
        $taskFolder = $taskService.GetFolder("\")
        $registeredTask = $taskFolder.GetTask($taskName)
        $registeredTask.Enabled = $TRUE
        $taskDefinition = $registeredTask.Definition
        $taskDefinition.Triggers | % {$_.Enabled}
      5. Paste the PowerShell code below for the Remediation Script.
        $TASK_CREATE_OR_UPDATE = 0x6
        
        # Specify the computer name
        $computerName = "localhost"
        # Specify the task's name
        $taskName = "\Microsoft\Windows\Defrag\ScheduledDefrag"
        
        $taskService = new-object -comobject "Schedule.Service"
        $taskService.Connect($computerName)
        $taskFolder = $taskService.GetFolder("\")
        $registeredTask = $taskFolder.GetTask($taskName)
        $registeredTask.Enabled = $TRUE
        $taskDefinition = $registeredTask.Definition
        $taskDefinition.Triggers | foreach-object { $_.Enabled = $TRUE }
        [Void] $taskFolder.RegisterTaskDefinition($taskName, $taskDefinition,
         $TASK_CREATE_OR_UPDATE, $NULL, $NULL, $taskDefinition.Principal.LogonType)
      6. Create a new Compliance Rule tab of the configuration item, where Enabled = True, as shown next:
      7. Add this configuration item to a baseline and deploy to a collection. Note that in order to  execute Compliance Management scripts that use Powershell, you may need to modify the client setting named “PowerShell Execution Policy”  to Bypass.

Download EnableDefragOnServer08andWindows7.ps1.


Filed under: Compliance Settings, ConfigMgr 2012, PowerShell

email

Written by , Posted .